-
Notifications
You must be signed in to change notification settings - Fork 180
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0bb32aa
Initial compiling ZeroCopyFromV2
sffc 3d19612
Update more uses of ZCF
sffc bb8454a
Fix all compiling in yoke crate
sffc eeb564a
Fix zerovec
sffc 6a485c6
Fix remaining build issues
sffc 96d5f75
Use 'zcf for the lifetime parameter
sffc c7e5d9e
Another 'zcf
sffc 2655d63
Simplification and review feedback
sffc f14b3fe
Use 'static instead of 'zcf in bounds
sffc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] | ||
|
@@ -35,57 +36,43 @@ use stable_deref_trait::StableDeref; | |
/// message: Cow<'data, str>, | ||
/// } | ||
/// | ||
/// unsafe impl<'a> Yokeable<'a> for MyStruct<'static> { | ||
/// // (not shown; see `Yokeable` for examples) | ||
/// # type Output = MyStruct<'a>; | ||
/// # fn transform(&'a self) -> &'a Self::Output { | ||
/// # self | ||
/// # } | ||
/// # fn transform_owned(self) -> MyStruct<'a> { | ||
/// # // covariant lifetime cast, can be done safely | ||
/// # self | ||
/// # } | ||
/// # unsafe fn make(from: Self::Output) -> Self { | ||
/// # std::mem::transmute(from) | ||
/// # } | ||
/// # fn transform_mut<F>(&'a mut self, f: F) | ||
/// # where | ||
/// # F: 'static + for<'b> FnOnce(&'b mut Self::Output), | ||
/// # { | ||
/// # unsafe { | ||
/// # f(std::mem::transmute::<&'a mut Self, &'a mut Self::Output>( | ||
/// # self, | ||
/// # )) | ||
/// # } | ||
/// # } | ||
/// } | ||
/// | ||
/// // 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<'zcf, C: ?Sized, T> ZeroCopyFrom<'zcf, C> for YokeTraitHack<T> | ||
where | ||
T: ZeroCopyFrom<'zcf, C>, | ||
{ | ||
#[inline] | ||
fn zero_copy_from(cart: &'zcf C) -> Self { | ||
YokeTraitHack(T::zero_copy_from(cart)) | ||
} | ||
} | ||
|
||
impl<'b, 's, Y, C> Yoke<Y, C> | ||
impl<Y, C> Yoke<Y, C> | ||
where | ||
Y: for<'a> Yokeable<'a> + ZeroCopyFrom<<C as Deref>::Target>, | ||
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 +97,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 +110,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 +165,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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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