Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.Β Read More..

FREE Email Marketing Course

Signature for a Rust method that modifies object but might also drop it?

February 22, 2019 By admin

Software https://softwareengineering.stackexchange.com/questions/387575/signature-for-a-rust-method-that-modifies-object-but-might-also-drop-it

I’w writing an app in Rust that essentially pushes data through a pipeline of steps that transforms it in ways that are different. The information is represented by an Entry, and I am designing a Measure trait for the measures in the pipeline.

The trait should have a method that process the entry. My initial design was this:

Fn process(&self, entry: &mut Entrance ) -> ()

Afterwards, I realize I needed some measures to have the ability to shed the entry, i.e. somehow signal to the caller which the entry shouldn’t be processed further through the pipeline. I’m considering two signatures for this:

Fn procedure (itself, entry: Entry) -> Choice  //Returns None if object is dropped.
Fn process(itself, entry: &mut Entry) -> bool  //Returns false if object is lost.

Which would you say is your most idiomatic and strangest? Is there some performance penalty in passing the ownership around as in the first option? Is there a better alternative?