-
Notifications
You must be signed in to change notification settings - Fork 176
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
Changes from 7 commits
0bb32aa
3d19612
bb8454a
eeb564a
6a485c6
96d5f75
c7e5d9e
2655d63
f14b3fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
pub fn assert_zcf_generics<'a, 'b>( | ||
x: &'b ZeroVecExampleWithGenerics<'a, u8>, | ||
) -> ZeroVecExampleWithGenerics<'b, u8> { | ||
ZeroVecExampleWithGenerics::<'static, u8>::zero_copy_from(x) | ||
ZeroVecExampleWithGenerics::<'b, u8>::zero_copy_from(x) | ||
} | ||
|
||
// Since ZeroMap has generic parameters, the Rust compiler cannot | ||
|
@@ -78,7 +78,7 @@ pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> { | |
pub fn assert_zcf_map<'a, 'b>( | ||
x: &'b ZeroMapGenericExample<'a, str>, | ||
) -> ZeroMapGenericExample<'b, str> { | ||
ZeroMapGenericExample::<'static, str>::zero_copy_from(x) | ||
ZeroMapGenericExample::<'b, str>::zero_copy_from(x) | ||
} | ||
|
||
#[derive(Yokeable, Clone, ZeroCopyFrom)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
use crate::trait_hack::YokeTraitHack; | ||
use crate::Yoke; | ||
use crate::Yokeable; | ||
#[cfg(feature = "alloc")] | ||
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the yokeable impl is no longer necessary in the docs above |
||
/// fn zero_copy_from(cart: &'zcf MyStruct<'_>) -> Self { | ||
/// MyStruct { | ||
/// message: Cow::Borrowed(&cart.message) | ||
/// } | ||
/// } | ||
/// } | ||
/// | ||
/// // Reference from a string slice directly | ||
/// impl ZeroCopyFrom<str> for MyStruct<'static> { | ||
/// fn zero_copy_from<'b>(cart: &'b str) -> MyStruct<'b> { | ||
/// impl<'zcf> ZeroCopyFrom<'zcf, str> for MyStruct<'zcf> { | ||
/// fn zero_copy_from(cart: &'zcf str) -> Self { | ||
/// MyStruct { | ||
/// message: Cow::Borrowed(cart) | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
pub trait ZeroCopyFrom<C: ?Sized>: for<'a> Yokeable<'a> { | ||
/// Clone the cart `C` into a [`Yokeable`] struct, which may retain references into `C`. | ||
fn zero_copy_from<'b>(cart: &'b C) -> <Self as Yokeable<'b>>::Output; | ||
pub trait ZeroCopyFrom<'zcf, C: ?Sized>: 'zcf { | ||
/// Clone the cart `C` into a struct that may retain references into `C`. | ||
fn zero_copy_from(cart: &'zcf C) -> Self; | ||
} | ||
|
||
impl<'b, 's, Y, C> Yoke<Y, C> | ||
impl<'zcf, C: ?Sized, T> ZeroCopyFrom<'zcf, C> for YokeTraitHack<T> | ||
where | ||
Y: for<'a> Yokeable<'a> + ZeroCopyFrom<<C as Deref>::Target>, | ||
T: ZeroCopyFrom<'zcf, C>, | ||
{ | ||
#[inline] | ||
fn zero_copy_from(cart: &'zcf C) -> Self { | ||
YokeTraitHack(T::zero_copy_from(cart)) | ||
} | ||
} | ||
|
||
impl<Y, C> Yoke<Y, C> | ||
where | ||
Y: for<'a> Yokeable<'a>, | ||
for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: ZeroCopyFrom<'a, <C as Deref>::Target>, | ||
C: StableDeref + Deref, | ||
{ | ||
/// Construct a [`Yoke`]`<Y, C>` from a cart implementing `StableDeref` by zero-copy cloning | ||
|
@@ -110,9 +122,10 @@ where | |
/// | ||
/// assert_eq!("demo", yoke.get()); | ||
/// ``` | ||
#[inline] | ||
pub fn attach_to_zero_copy_cart(cart: C) -> Self { | ||
Yoke::<Y, C>::attach_to_cart_badly(cart, Y::zero_copy_from) | ||
Yoke::<Y, C>::attach_to_cart(cart, |c| { | ||
YokeTraitHack::<<Y as Yokeable>::Output>::zero_copy_from(c).0 | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -122,47 +135,47 @@ where | |
// specialization. | ||
|
||
#[cfg(feature = "alloc")] | ||
impl ZeroCopyFrom<str> for Cow<'static, str> { | ||
impl<'zcf> ZeroCopyFrom<'zcf, str> for Cow<'zcf, str> { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b str) -> Cow<'b, str> { | ||
fn zero_copy_from(cart: &'zcf str) -> Self { | ||
Cow::Borrowed(cart) | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl ZeroCopyFrom<String> for Cow<'static, str> { | ||
impl<'zcf> ZeroCopyFrom<'zcf, String> for Cow<'zcf, str> { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b String) -> Cow<'b, str> { | ||
fn zero_copy_from(cart: &'zcf String) -> Self { | ||
Cow::Borrowed(cart) | ||
} | ||
} | ||
|
||
impl ZeroCopyFrom<str> for &'static str { | ||
impl<'zcf> ZeroCopyFrom<'zcf, str> for &'zcf str { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b str) -> &'b str { | ||
fn zero_copy_from(cart: &'zcf str) -> Self { | ||
cart | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl ZeroCopyFrom<String> for &'static str { | ||
impl<'zcf> ZeroCopyFrom<'zcf, String> for &'zcf str { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b String) -> &'b str { | ||
fn zero_copy_from(cart: &'zcf String) -> Self { | ||
cart | ||
} | ||
} | ||
|
||
impl<C, T: ZeroCopyFrom<C>> ZeroCopyFrom<Option<C>> for Option<T> { | ||
fn zero_copy_from<'b>(cart: &'b Option<C>) -> Option<<T as Yokeable<'b>>::Output> { | ||
impl<'zcf, C, T: ZeroCopyFrom<'zcf, C>> ZeroCopyFrom<'zcf, Option<C>> for Option<T> { | ||
fn zero_copy_from(cart: &'zcf Option<C>) -> Self { | ||
cart.as_ref() | ||
.map(|c| <T as ZeroCopyFrom<C>>::zero_copy_from(c)) | ||
} | ||
} | ||
|
||
impl<C1, T1: ZeroCopyFrom<C1>, C2, T2: ZeroCopyFrom<C2>> ZeroCopyFrom<(C1, C2)> for (T1, T2) { | ||
fn zero_copy_from<'b>( | ||
cart: &'b (C1, C2), | ||
) -> (<T1 as Yokeable<'b>>::Output, <T2 as Yokeable<'b>>::Output) { | ||
impl<'zcf, C1, T1: ZeroCopyFrom<'zcf, C1>, C2, T2: ZeroCopyFrom<'zcf, C2>> | ||
ZeroCopyFrom<'zcf, (C1, C2)> for (T1, T2) | ||
{ | ||
fn zero_copy_from(cart: &'zcf (C1, C2)) -> Self { | ||
( | ||
<T1 as ZeroCopyFrom<C1>>::zero_copy_from(&cart.0), | ||
<T2 as ZeroCopyFrom<C2>>::zero_copy_from(&cart.1), | ||
|
@@ -177,23 +190,23 @@ impl<C1, T1: ZeroCopyFrom<C1>, C2, T2: ZeroCopyFrom<C2>> ZeroCopyFrom<(C1, C2)> | |
// or inference are involved, and the proc macro does not necessarily have | ||
// enough type information to figure this out on its own. | ||
#[cfg(feature = "alloc")] | ||
impl<B: ToOwned + ?Sized + 'static> ZeroCopyFrom<Cow<'_, B>> for Cow<'static, B> { | ||
impl<'zcf, B: ToOwned + ?Sized> ZeroCopyFrom<'zcf, Cow<'_, B>> for Cow<'zcf, B> { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b Cow<'_, B>) -> Cow<'b, B> { | ||
fn zero_copy_from(cart: &'zcf Cow<'_, B>) -> Self { | ||
Cow::Borrowed(cart) | ||
} | ||
} | ||
|
||
impl ZeroCopyFrom<&'_ str> for &'static str { | ||
impl<'zcf> ZeroCopyFrom<'zcf, &'_ str> for &'zcf str { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b &'_ str) -> &'b str { | ||
fn zero_copy_from(cart: &'zcf &'_ str) -> &'zcf str { | ||
cart | ||
} | ||
} | ||
|
||
impl<T: 'static> ZeroCopyFrom<[T]> for &'static [T] { | ||
impl<'zcf, T> ZeroCopyFrom<'zcf, [T]> for &'zcf [T] { | ||
#[inline] | ||
fn zero_copy_from<'b>(cart: &'b [T]) -> &'b [T] { | ||
fn zero_copy_from(cart: &'zcf [T]) -> &'zcf [T] { | ||
cart | ||
} | ||
} |
There was a problem hiding this comment.
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