Wednesday 15 January 2014

rust - Cannot move out of dereference of `&`-pointer -



rust - Cannot move out of dereference of `&`-pointer -

i have next code

pub struct propertydeclarationblock { pub declarations: arc<vec<(propertydeclaration, propertydeclarationimportance)>> } impl propertydeclarationblock { pub fn select_declarations(&self) -> arc<vec<propertydeclaration>> { arc::new(self.declarations.clone().map_in_place(|p| { allow (declaration, _) = p; declaration })) } }

i want able phone call .select_declarations() on propertydeclarationblock , have homecoming clone of declarations instead of beingness arc vec (propertydeclaration, propertydeclarationimportance) arc vec propertydeclaration, in other words returning a vector of propertydeclaration instead of previous tuple.

the previous won't compile getting next error:

error: cannot move out of dereference of `&`-pointer arc::new(self.declarations.clone().map_in_place(|p| { ^~~~~~~~~~~~~~~~~~~~~~~~~

from understand, since function take self parameter take ownership of it. since i'd rather have function burrow self utilize &.

edit

here error message after implementing new function:

error: cannot move out of dereference of `&`-pointer arc::new(self.declarations.iter().map(|&(declaration, _)| declaration).collect()) ^~~~~~~~~~~~~~~~~ note: attempting move value here (to prevent move, utilize `ref declaration` or `ref mut declaration` capture value reference) arc::new(self.declarations.iter().map(|&(declaration, _)| declaration).collect()) ^~~~~~~~~~~

i tried applying ref keyword before declaration suggested error message didn't help.

self.declarations.clone() clones arc, while believe intended clone vec. resolve phone call map_in_place, compiler automatically dereferences arc able phone call method, defined on vec. compiler knows how dereference arc because arc implements deref. deref returns borrowed pointer, , error comes from. clone vec, must explicitly dereference arc: (*self.declarations).clone().

however, map_in_place not suitable in case, (propertydeclaration, propertydeclarationimportance) doesn't have same size propertydeclaration (unless propertydeclarationimportance has size of zero, not case), required per docs. fails message similar this:

task '<main>' failed @ 'assertion failed: mem::size_of::<t>() == mem::size_of::<u>()', /build/rust-git/src/rust/src/libcollections/vec.rs:1805

here's proper implementation:

impl propertydeclarationblock { pub fn select_declarations(&self) -> arc<vec<propertydeclaration>> { arc::new(self.declarations.iter().map(|&(declaration, _)| declaration).collect()) } }

here, utilize iter on vec create iterator on vector's items. returns items, implements iterator on immutable references.

then, utilize map lazily map (propertydeclaration, propertydeclarationimportance) propertydeclaration. note how destructure tuple and reference in closure's parameter list (this works in fns too) instead of using let statement.

finally, utilize collect create new collection container sequence. collect's result generic; can type implements fromiterator. vec implements fromiterator, , compiler infers vec method's signature.

rust

No comments:

Post a Comment