I have design issue that I would like solve with safe Rust that I haven't been able to find a viable solution. I can't use a RefCell
because you can't get a & reference to the data, only Ref
/ RefMut
.
Here is a simplified example with irrelevant fields / methods removed
use std::cell::RefCell;
use std::rc::Rc;
struct LibraryStruct {}
impl LibraryStruct {
fn function(&self, _a: &TraitFromLibrary) {}
}
trait TraitFromLibrary {
fn trait_function(&self, library_struct: LibraryStruct);
}
// I don't want to copy this, bad performance
struct A {
// fields...
}
impl TraitFromLibrary for A {
fn trait_function(&self, library_struct: LibraryStruct) {
// custom A stuff
}
}
// B manipulates A's in data
struct B {
data: Vec<A>,
}
struct C {
// This type doesn't have to be & for solution. C just needs immutable access
a: Rc<RefCell<A>>,
}
impl<'a> TraitFromLibrary for C {
fn trait_function(&self, library_struct: LibraryStruct) {
// custom C stuff
// Takes generic reference &, this is why Ref / RefCell doesn't work
library_struct.function(&self.a.borrow());
}
}
// B and C's constructed in Container and lifetime matches Container
// Container manipulates fields b and c
struct Container {
b: B,
c: Vec<C>,
}
fn main() {}
I would be able to solve this with Rc<RefCell<A>>
but I am being restricted from the library requiring &A
.
This produces the error:
error[E0277]: the trait bound `std::cell::Ref<'_, A>: TraitFromLibrary` is not satisfied
--> src/main.rs:33:33
|
33 | library_struct.function(&self.a.borrow());
| ^^^^^^^^^^^^^^^^ the trait `TraitFromLibrary` is not implemented for `std::cell::Ref<'_, A>`
|
= note: required for the cast to the object type `TraitFromLibrary`
&'a A
. Struct B and C exist at the same time in Container and there would be a mutability issue as there would be an immutable reference in C and B needs mutability at times. – JakeC
'sa
to be aRefCell
, but that won't work with the library function? – Peter HallRc<RefCell<A>>
and then pass it to the library function aslibrary_struct.function(&self.a.borrow());
– Peter HallRef
implementsDeref
. If a function accepts a&
-reference, then you can pass a reference to any type that implementsDeref
, as in this example. – Peter Hall