-
Consider this example: struct SuperSet {
a: MyStructOne,
b: MyStructTwo,
c: MyStructThree,
}
struct SubSet {
a: &'static MyStructOne,
c: &'static MyStructThree,
}
fn convert(value: yoke::Yoke<&'static SuperSet, yoke::erased::ErasedRcCart>) -> SubSet {
value.map_project(|SuperSet { a, b: _, c }, _| { SubSet { a, b })
} This doesn't compile because neither SuperSet nor SubSet are #[derive(yoke::Yokeable)]
struct SuperSet {
a: &'static MyStructOne,
b: &'static MyStructTwo,
c: &'static MyStructThree,
}
#[derive(yoke::Yokeable)]
struct SubSet {
a: &'static MyStructOne,
c: &'static MyStructThree,
} The conversion works, but constructing impl SuperSet {
fn new() -> yoke::Yoke<Self, yoke::erased::ErasedRcCart> {
yoke::Yoke::attach_to_cart(
std::rc::Rc::new((Default::default(), Default::default(), Default::default())),
|(a, b, c)| Self { a, b, c },
)
.erase_rc_cart()
}
}
I'm sure I'm holding it wrong, but not sure how to hold it correctly. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
The data should be owned by the cart and borrowed by the Yokeable. So you want something more like this: struct OwnedSuperSet {
a: MyStructOne,
b: MyStructTwo,
c: MyStructThree,
}
#[derive(Yokeable)]
struct BorrowedSuperSet<'a> {
a: &'a MyStructOne,
b: &'a MyStructTwo,
c: &'a MyStructThree,
}
#[derive(Yokeable)]
struct BorrowedSubSet<'a> {
a: &'a MyStructOne,
c: &'a MyStructThree,
}
// Start here:
Yoke<BorrowedSuperSet<'static>, Rc<OwnedSuperSet>>
// map_project to here:
Yoke<BorrowedSubSet<'static>, Rc<OwnedSuperSet>> |
Beta Was this translation helpful? Give feedback.
-
Yep, you can also make it work in an easier way with Cow fields (and you can use the |
Beta Was this translation helpful? Give feedback.
The data should be owned by the cart and borrowed by the Yokeable.
So you want something more like this: