dtolnay / proc-macro-workshop

Learn to write Rust procedural macros  [Rust Latam conference, Montevideo Uruguay, March 2019]
Apache License 2.0
4k stars 1.01k forks source link

The build function in Builder derive macro should take ownership #70

Open dupeiran001 opened 5 months ago

dupeiran001 commented 5 months ago

In the build derive macro, 04-call-builder, the build function takes a &mut self, and it returns a Command with the ownership of the fields. So I have to clone the fields in the implementation, which is not correct I suppose.

The build function should take the ownership of Builder, and return a builded result.

like this:

fn build(self) -> Result<Command, dyn Error>{
    unimplemented!()
}
shouya commented 3 months ago

So I have to clone the fields in the implementation

To avoid cloning, you can look into Option::take as well as Vec::split_off.