I wish to use an external struct
(Framer) which &'a mut
borrows a number of parameters in its new()
method. This is a problem when I insert an instance of the struct in a HashMap with a larger scope. I understand why.
I wrapped the offending struct
in my own struct
in an attempt to overcome the problem - but again ran into ownership problems. Playground
struct Owned<'b> {
buf: Box<[u8]>,
foo: Option<Foo<'b>>,
}
impl<'b> Owned<'b> {
pub fn new() -> Self {
let mut owned = Self {
buf: Box::new([0; 1000]),
foo: None,
};
owned.foo = Some(Foo::new(&mut *owned.buf));
owned
/*
^^^^^ returns a value referencing data owned by the current function
^^^^^ returning this value requires that `*owned.buf` is borrowed for `'a`
*/
}
}
I understand why returning owned
exposes the borrowed buf
. But, I feel that I should be able link the lifetime of owned.buf to owned.foo - but I cannot see how.
I would like to know:
- How to wrap Foo in Owned successfully
- Or alternatively, some better way to insert Foo in a HashMap from a larger scope.