rust - &self move field containing Box - Move out of borrowed content -
i have struct field containing references other structs (that did not define)
struct htmlhandlebars { user_helpers: vec<(string, box<helperdef + 'static>)>, }
and htmlhandlebars
has implement function
fn render(&self, ...) -> &self
and in function need move box
function. this:
fn render(&self, ...) -> &self { let mut handlebars = handlebars::new(); (name, helper) in self.user_helpers { handlebars.register_helper(&name, helper); } }
but kind of stuck because:
- i can't move box references because borrowing
self
- i can't copy box references because struct not implement copy
- i can't modify
&self
&mut self
because causes other problems...
maybe doing wrong.. there else can do? options?
if need more complete overview of code, can find here
ps: had no idea how describe situation in title, feel free change it
the code you've written trying consume vec
, elements. in general, can iterate on &self.user_helpers
give references elements rather consuming them. (modulo silly typos in pattern):
for &(ref name, ref helper) in self.user_helpers { handlebars.register_helper(name, helper); }
see also: the rust programming language on iterating vectors.
there problem though: handlebars needs ownership of helpers. may have create new handlebars
object every time render, in case need able create helpers every time create new handlebars
. there no way take ownership of boxes without taking @ least &mut
reference vec
. need @ least mutable access take handlers out of struct. , if did that, wouldn't have handlers around next time render()
called. depending on how configurable set of handlers is, have function constructs vec<box<helperdef + 'static>>
out of thin air when need it, or maintain list of callbacks construct box<helperdef + 'static>
you.
Comments
Post a Comment