Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Yokeable::Output from ZeroCopyFrom trait #1499

Merged
merged 9 commits into from
Jan 12, 2022
Merged

Conversation

sffc
Copy link
Member

@sffc sffc commented Jan 12, 2022

Fixes #1255
Replaces #1257

Using this trait:

pub trait ZeroCopyFromV2<'a, C: ?Sized>: 'a {
    fn zero_copy_from_v2(cart: &'a C) -> Self;
}

And this function signature:

impl<Y, C> Yoke<Y, C>
where
    Y: for<'a> Yokeable<'a>,
    for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: ZeroCopyFromV2<'a, <C as Deref>::Target>,
    C: StableDeref + Deref,
{
    pub fn attach_to_zero_copy_v2_cart(cart: C) -> Self {
        Yoke::<Y, C>::attach_to_cart(cart, |c| {
            YokeTraitHack::<<Y as Yokeable>::Output>::zero_copy_from_v2(c).0
        })
    }
}

I managed to get this to compile:

    let yoke = Yoke::<
        Cow<'static, str>,
        String
    >::attach_to_zero_copy_v2_cart("demo".to_string());

@sffc sffc requested a review from Manishearth January 12, 2022 02:39
@sffc
Copy link
Member Author

sffc commented Jan 12, 2022

Without the YokeTraitHack, I was getting errors like

error[E0277]: the trait bound `for<'a> <Cow<'static, str> as Yokeable<'a>>::Output: ZeroCopyFromV2<'a, str>` is not satisfied
   --> utils/yoke/src/zero_copy_from.rs:313:16
    |
313 |       let yoke = Yoke::<
    |  ________________^
314 | |         Cow<'static, str>,
315 | |         String
316 | |     >::attach_to_zero_copy_v2_cart("demo".to_string());
    | |__________________________________^ the trait `for<'a> ZeroCopyFromV2<'a, str>` is not implemented for `<Cow<'static, str> as Yokeable<'a>>::Output`
    |
note: required by `zero_copy_from::<impl Yoke<Y, C>>::attach_to_zero_copy_v2_cart`
   --> utils/yoke/src/zero_copy_from.rs:151:5
    |
151 |     pub fn attach_to_zero_copy_v2_cart(cart: C) -> Self {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider further restricting the associated type
    |
312 | fn test_v2() where for<'a> <Cow<'static, str> as Yokeable<'a>>::Output: ZeroCopyFromV2<'a, str> {
    |              ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

@sffc sffc changed the title Initial compiling ZeroCopyFromV2 Remove Yokeable::Output from ZeroCopyFrom trait Jan 12, 2022
@sffc sffc marked this pull request as ready for review January 12, 2022 09:15
Manishearth
Manishearth previously approved these changes Jan 12, 2022
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, minor issue

@@ -61,8 +61,8 @@ impl Deref for LineBreakPropertyTable<'_> {
}
}

impl<'a> ZeroCopyFrom<LineBreakPropertyTable<'a>> for LineBreakPropertyTable<'static> {
fn zero_copy_from<'b>(cart: &'b LineBreakPropertyTable<'a>) -> <Self as Yokeable<'b>>::Output {
impl<'zcf> ZeroCopyFrom<'zcf, LineBreakPropertyTable<'_>> for LineBreakPropertyTable<'zcf> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: good call on 'zcf

@@ -51,12 +51,12 @@ pub struct HasTuples<'data> {
}

pub fn assert_zcf_tuples<'b, 'data>(x: &'b HasTuples<'data>) -> HasTuples<'b> {
HasTuples::<'static>::zero_copy_from(x)
HasTuples::<'b>::zero_copy_from(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is this explicit lt still necessary? one of the benefits of the simplification is that a bunch of these won't be needed anymore

for<'b> Yokeable<'b, Output = <K as ZeroMapKV<'b>>::Container>,
<V as ZeroMapKV<'static>>::Container:
for<'b> Yokeable<'b, Output = <V as ZeroMapKV<'b>>::Container>,
<K as ZeroMapKV<'a>>::Container: ZeroCopyFrom<'a, <K as ZeroMapKV<'s>>::Container>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: ah, these bounds get much simpler, i was hoping this would be the case

@@ -61,31 +62,42 @@ use stable_deref_trait::StableDeref;
/// }
///
/// // Reference from a borrowed version of self
/// impl<'data> ZeroCopyFrom<MyStruct<'data>> for MyStruct<'static> {
/// fn zero_copy_from<'b>(cart: &'b MyStruct<'data>) -> MyStruct<'b> {
/// impl<'zcf> ZeroCopyFrom<'zcf, MyStruct<'_>> for MyStruct<'zcf> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the yokeable impl is no longer necessary in the docs above

impl<'a, T: 'static + AsULE + ?Sized> ZeroCopyFrom<'a, ZeroVec<'_, T>> for ZeroVec<'a, T> {
impl<'zcf, T> ZeroCopyFrom<'zcf, ZeroVec<'_, T>> for ZeroVec<'zcf, T>
where
T: 'zcf + AsULE + ?Sized,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: not sure if T: 'zcf makes sense here, but aiui it doesn't cause harm to have

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need either T: 'zcf or T: 'static to make the compiler happy when compiling ZeroVec<'zcf, T>, so I figured we should use the less restrictive lifetime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it should be 'static for now, because a non-'static AsULE doesn't quite make sense (.get() can't work), and it seems misleading to have a 'zcf bound that isn't actually useful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done.

V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
<K as ZeroMapKV<'a>>::Container: ZeroCopyFrom<'a, <K as ZeroMapKV<'s>>::Container>,
<V as ZeroMapKV<'a>>::Container: ZeroCopyFrom<'a, <V as ZeroMapKV<'s>>::Container>,
K: 'zcf + for<'b> ZeroMapKV<'b> + ?Sized,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto: does the 'zcf bound actually get us anything?

@sffc sffc requested a review from Manishearth January 12, 2022 21:03
@sffc sffc merged commit 57c44dd into unicode-org:main Jan 12, 2022
@sffc sffc deleted the zcf_v2 branch January 12, 2022 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Attempt to simplify ZeroCopyFrom
2 participants