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

ZeroCopyFrom simplification attempt #1257

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions utils/yoke/src/macro_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ macro_rules! impl_copy_type {
type Output = Self;
copy_yoke_impl!();
}
impl ZeroCopyFrom<$ty> for $ty {
fn zero_copy_from(this: &Self) -> Self {
impl<'b> ZeroCopyFrom<'b, $ty> for $ty {
fn zero_copy_from(this: &'b Self) -> Self {
// Essentially only works when the struct is fully Copy
*this
}
Expand All @@ -61,8 +61,9 @@ unsafe impl<'a, T: Copy + 'static, const N: usize> Yokeable<'a> for [T; N] {
type Output = Self;
copy_yoke_impl!();
}
impl<T: Copy + 'static, const N: usize> ZeroCopyFrom<[T; N]> for [T; N] {
fn zero_copy_from(this: &Self) -> Self {

impl<'b, T: Copy + 'static, const N: usize> ZeroCopyFrom<'b, [T; N]> for [T; N] {
fn zero_copy_from(this: &'b Self) -> Self {
*this
}
}
Expand Down
111 changes: 72 additions & 39 deletions utils/yoke/src/zero_copy_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,32 @@ use alloc::string::String;
/// }
///
/// // 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<'data> ZeroCopyFrom<'data, MyStruct<'_>> for MyStruct<'data> {
/// fn zero_copy_from(cart: &'data MyStruct<'_>) -> MyStruct<'data> {
/// 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<'data> ZeroCopyFrom<'data, str> for MyStruct<'data> {
/// fn zero_copy_from(cart: &'data str) -> MyStruct<'data> {
/// MyStruct {
/// message: Cow::Borrowed(cart)
/// }
/// }
/// }
/// ```
pub trait ZeroCopyFrom<C: ?Sized>: for<'a> Yokeable<'a> {
pub trait ZeroCopyFrom<'b, C: ?Sized> {
/// 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;
fn zero_copy_from(cart: &'b C) -> Self;
}

impl<'b, 's, Y: ZeroCopyFrom<C> + for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, &'b C> {
impl<'b, 's, Y: for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, &'b C>
where
for<'a> <Y as Yokeable<'a>>::Output: ZeroCopyFrom<'a, C>,
{
/// Construct a [`Yoke`]`<Y, &C>` from a borrowed cart by zero-copy cloning the cart to `Y` and
/// then yokeing that object to the cart.
///
Expand All @@ -107,12 +110,23 @@ impl<'b, 's, Y: ZeroCopyFrom<C> + for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, &'b C
/// assert_eq!("demo", yoke.get());
/// ```
pub fn attach_to_borrowed_cart(cart: &'b C) -> Self {
Yoke::<Y, &'b C>::attach_to_cart_badly(cart, Y::zero_copy_from)
Yoke::<Y, &'b C>::attach_to_cart_badly(cart, Self::attach_borrowed_inner)
}

#[inline]
// These functions exist so that we can get the appropriate higher order function
// while using generics from the outer scope. Rust doesn't support partial
// lowering of higher order functions.
fn attach_borrowed_inner<'a>(c: &'a C) -> <Y as Yokeable<'a>>::Output {
Y::Output::zero_copy_from(c)
}
}

#[cfg(feature = "alloc")]
impl<'b, 's, Y: ZeroCopyFrom<C> + for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, Box<C>> {
impl<'b, 's, Y: for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, Box<C>>
where
for<'a> <Y as Yokeable<'a>>::Output: ZeroCopyFrom<'a, C>,
{
/// Construct a [`Yoke`]`<Y, Box<C>>` from a boxed cart by zero-copy cloning the cart to `Y` and
/// then yokeing that object to the cart.
///
Expand All @@ -137,12 +151,23 @@ impl<'b, 's, Y: ZeroCopyFrom<C> + for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, Box<C
/// assert_eq!("demo", yoke.get());
/// ```
pub fn attach_to_box_cart(cart: Box<C>) -> Self {
Yoke::<Y, Box<C>>::attach_to_cart_badly(cart, Y::zero_copy_from)
Yoke::<Y, Box<C>>::attach_to_cart_badly(cart, Self::attach_boxed_inner)
}

#[inline]
// These functions exist so that we can get the appropriate higher order function
// while using generics from the outer scope. Rust doesn't support partial
// lowering of higher order functions.
fn attach_boxed_inner<'a>(c: &'a C) -> <Y as Yokeable<'a>>::Output {
Y::Output::zero_copy_from(c)
}
}

#[cfg(feature = "alloc")]
impl<'b, 's, Y: ZeroCopyFrom<C> + for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, Rc<C>> {
impl<'b, 's, Y: for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, Rc<C>>
where
for<'a> <Y as Yokeable<'a>>::Output: ZeroCopyFrom<'a, C>,
{
/// Construct a [`Yoke`]`<Y, Rc<C>>` from a reference-counted cart by zero-copy cloning the
/// cart to `Y` and then yokeing that object to the cart.
///
Expand All @@ -168,56 +193,64 @@ impl<'b, 's, Y: ZeroCopyFrom<C> + for<'a> Yokeable<'a>, C: ?Sized> Yoke<Y, Rc<C>
/// assert_eq!("demo", yoke.get());
/// ```
pub fn attach_to_rc_cart(cart: Rc<C>) -> Self {
Yoke::<Y, Rc<C>>::attach_to_cart_badly(cart, Y::zero_copy_from)
Yoke::<Y, Rc<C>>::attach_to_cart_badly(cart, Self::attach_rc_inner)
}

#[inline]
// These functions exist so that we can get the appropriate higher order function
// while using generics from the outer scope. Rust doesn't support partial
// lowering of higher order functions.
fn attach_rc_inner<'a>(c: &'a C) -> <Y as Yokeable<'a>>::Output {
Y::Output::zero_copy_from(c)
}
}

// Note: The following could be blanket implementations, but that would require constraining the
// blanket `T` on `T: 'static`, which may not be desirable for all downstream users who may wish
// to customize their `ZeroCopyFrom` impl. The blanket implementation may be safe once Rust has
// specialization.
// // Note: The following could be blanket implementations, but that would require constraining the
// // blanket `T` on `T: 'static`, which may not be desirable for all downstream users who may wish
// // to customize their `ZeroCopyFrom` impl. The blanket implementation may be safe once Rust has
// // specialization.

#[cfg(feature = "alloc")]
impl ZeroCopyFrom<str> for Cow<'static, str> {
fn zero_copy_from<'b>(cart: &'b str) -> Cow<'b, str> {
impl<'b> ZeroCopyFrom<'b, str> for Cow<'b, str> {
fn zero_copy_from(cart: &'b str) -> Cow<'b, str> {
Cow::Borrowed(cart)
}
}

#[cfg(feature = "alloc")]
impl ZeroCopyFrom<String> for Cow<'static, str> {
fn zero_copy_from<'b>(cart: &'b String) -> Cow<'b, str> {
impl<'b> ZeroCopyFrom<'b, String> for Cow<'b, str> {
fn zero_copy_from(cart: &'b String) -> Cow<'b, str> {
Cow::Borrowed(cart)
}
}

impl ZeroCopyFrom<str> for &'static str {
fn zero_copy_from<'b>(cart: &'b str) -> &'b str {
impl<'b> ZeroCopyFrom<'b, str> for &'b str {
fn zero_copy_from(cart: &'b str) -> &'b str {
cart
}
}

#[cfg(feature = "alloc")]
impl ZeroCopyFrom<String> for &'static str {
fn zero_copy_from<'b>(cart: &'b String) -> &'b str {
impl<'b> ZeroCopyFrom<'b, String> for &'b str {
fn zero_copy_from(cart: &'b String) -> &'b str {
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<'b, C, T: ZeroCopyFrom<'b, C>> ZeroCopyFrom<'b, Option<C>> for Option<T> {
fn zero_copy_from(cart: &'b Option<C>) -> Option<T> {
cart.as_ref()
.map(|c| <T as ZeroCopyFrom<C>>::zero_copy_from(c))
.map(|c| <T as ZeroCopyFrom<'b, 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<'b, C1, T1: ZeroCopyFrom<'b, C1>, C2, T2: ZeroCopyFrom<'b, C2>> ZeroCopyFrom<'b, (C1, C2)>
for (T1, T2)
{
fn zero_copy_from(cart: &'b (C1, C2)) -> (T1, T2) {
(
<T1 as ZeroCopyFrom<C1>>::zero_copy_from(&cart.0),
<T2 as ZeroCopyFrom<C2>>::zero_copy_from(&cart.1),
<T1 as ZeroCopyFrom<'b, C1>>::zero_copy_from(&cart.0),
<T2 as ZeroCopyFrom<'b, C2>>::zero_copy_from(&cart.1),
)
}
}
Expand All @@ -229,20 +262,20 @@ 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> {
fn zero_copy_from<'b>(cart: &'b Cow<'_, B>) -> Cow<'b, B> {
impl<'b, B: ToOwned + ?Sized + 'static> ZeroCopyFrom<'b, Cow<'_, B>> for Cow<'b, B> {
fn zero_copy_from(cart: &'b Cow<'_, B>) -> Cow<'b, B> {
Cow::Borrowed(cart)
}
}

impl ZeroCopyFrom<&'_ str> for &'static str {
fn zero_copy_from<'b>(cart: &'b &'_ str) -> &'b str {
impl<'b> ZeroCopyFrom<'b, &'_ str> for &'b str {
fn zero_copy_from(cart: &'b &'_ str) -> &'b str {
cart
}
}

impl<T> ZeroCopyFrom<[T]> for &'static [T] {
fn zero_copy_from<'b>(cart: &'b [T]) -> &'b [T] {
impl<'b, T> ZeroCopyFrom<'b, [T]> for &'b [T] {
fn zero_copy_from(cart: &'b [T]) -> &'b [T] {
cart
}
}