Repository navigation
|
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. |
Answered by
sffc
Oct 24, 2023
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>> |
2 replies
Answer selected by
tamird
|
Yep, you can also make it work in an easier way with Cow fields (and you can use the |
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The data should be owned by the cart and borrowed by the Yokeable.
So you want something more like this: