// This way we can copy-paste Yokeable impls #![allow(unknown_lints)] // forgetting_copy_types #![allow(renamed_and_removed_lints)] // forgetting_copy_types #![allow(forgetting_copy_types)] #![allow(clippy::forget_copy)] #![allow(clippy::forget_non_drop)]
usecrate::flexzerovec::FlexZeroVec; usecrate::map::ZeroMapBorrowed; usecrate::map::ZeroMapKV; usecrate::map2d::ZeroMap2dBorrowed; usecrate::ule::*; usecrate::{VarZeroVec, ZeroMap, ZeroMap2d, ZeroVec}; use core::{mem, ptr}; use yoke::*;
// This impl is similar to the impl on Cow and is safe for the same reasons /// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate unsafeimpl<'a, T: 'static + AsULE + ?Sized> Yokeable<'a> for ZeroVec<'static, T> { type Output = ZeroVec<'a, T>; #[inline] fn transform(&'a self) -> &'a Self::Output { self
} #[inline] fn transform_owned(self) -> Self::Output { self
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
// This impl is similar to the impl on Cow and is safe for the same reasons /// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate unsafeimpl<'a, T: 'static + VarULE + ?Sized> Yokeable<'a> for VarZeroVec<'static, T> { type Output = VarZeroVec<'a, T>; #[inline] fn transform(&'a self) -> &'a Self::Output { self
} #[inline] fn transform_owned(self) -> Self::Output { self
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
// This impl is similar to the impl on Cow and is safe for the same reasons /// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate unsafeimpl<'a> Yokeable<'a> for FlexZeroVec<'static> { type Output = FlexZeroVec<'a>; #[inline] fn transform(&'a self) -> &'a Self::Output { self
} #[inline] fn transform_owned(self) -> Self::Output { self
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
/// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate #[allow(clippy::transmute_ptr_to_ptr)] unsafeimpl<'a, K, V> Yokeable<'a> for ZeroMap<'static, K, V> where
K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
<K as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
<V as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
{ type Output = ZeroMap<'a, K, V>; #[inline] fn transform(&'a self) -> &'a Self::Output { unsafe { // Unfortunately, because K and V are generic, rustc is // unaware that these are covariant types, and cannot perform this cast automatically. // We transmute it instead, and enforce the lack of a lifetime with the `K, V: 'static` bound
mem::transmute::<&Self, &Self::Output>(self)
}
} #[inline] fn transform_owned(self) -> Self::Output {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); unsafe { // Similar problem as transform(), but we need to use ptr::read since // the compiler isn't sure of the sizes let this = mem::ManuallyDrop::new(self); let ptr: *constSelf::Output = (&*this as *constSelf).cast();
ptr::read(ptr)
}
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
/// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate #[allow(clippy::transmute_ptr_to_ptr)] unsafeimpl<'a, K, V> Yokeable<'a> for ZeroMapBorrowed<'static, K, V> where
K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
&'static <K as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
&'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
{ type Output = ZeroMapBorrowed<'a, K, V>; #[inline] fn transform(&'a self) -> &'a Self::Output { unsafe { // Unfortunately, because K and V are generic, rustc is // unaware that these are covariant types, and cannot perform this cast automatically. // We transmute it instead, and enforce the lack of a lifetime with the `K, V: 'static` bound
mem::transmute::<&Self, &Self::Output>(self)
}
} #[inline] fn transform_owned(self) -> Self::Output {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); unsafe { // Similar problem as transform(), but we need to use ptr::read since // the compiler isn't sure of the sizes let this = mem::ManuallyDrop::new(self); let ptr: *constSelf::Output = (&*this as *constSelf).cast();
ptr::read(ptr)
}
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
/// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate #[allow(clippy::transmute_ptr_to_ptr)] unsafeimpl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2d<'static, K0, K1, V> where
K0: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
K1: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
<K0 as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
<K1 as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
<V as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
{ type Output = ZeroMap2d<'a, K0, K1, V>; #[inline] fn transform(&'a self) -> &'a Self::Output { unsafe { // Unfortunately, because K and V are generic, rustc is // unaware that these are covariant types, and cannot perform this cast automatically. // We transmute it instead, and enforce the lack of a lifetime with the `K0, K1, V: 'static` bound
mem::transmute::<&Self, &Self::Output>(self)
}
} #[inline] fn transform_owned(self) -> Self::Output {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); unsafe { // Similar problem as transform(), but we need to use ptr::read since // the compiler isn't sure of the sizes let this = mem::ManuallyDrop::new(self); let ptr: *constSelf::Output = (&*this as *constSelf).cast();
ptr::read(ptr)
}
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
/// This impl requires enabling the optional `yoke` Cargo feature of the `zerovec` crate #[allow(clippy::transmute_ptr_to_ptr)] unsafeimpl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2dBorrowed<'static, K0, K1, V> where
K0: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
K1: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
&'static <K0 as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
&'static <K1 as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
&'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
{ type Output = ZeroMap2dBorrowed<'a, K0, K1, V>; #[inline] fn transform(&'a self) -> &'a Self::Output { unsafe { // Unfortunately, because K and V are generic, rustc is // unaware that these are covariant types, and cannot perform this cast automatically. // We transmute it instead, and enforce the lack of a lifetime with the `K0, K1, V: 'static` bound
mem::transmute::<&Self, &Self::Output>(self)
}
} #[inline] fn transform_owned(self) -> Self::Output {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); unsafe { // Similar problem as transform(), but we need to use ptr::read since // the compiler isn't sure of the sizes let this = mem::ManuallyDrop::new(self); let ptr: *constSelf::Output = (&*this as *constSelf).cast();
ptr::read(ptr)
}
} #[inline] unsafefn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); let from = mem::ManuallyDrop::new(from); let ptr: *constSelf = (&*from as *constSelf::Output).cast();
ptr::read(ptr)
} #[inline] fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{ unsafe { f(mem::transmute::<&mutSelf, &mutSelf::Output>(self)) }
}
}
#[cfg(test)] #[allow(non_camel_case_types, non_snake_case)] mod test { usesuper::*; usecrate::{vecs::FlexZeroSlice, VarZeroSlice, ZeroSlice}; use databake::*;
// Note: The following derives cover Yoke as well as Serde and databake. These may partially // duplicate tests elsewhere in this crate, but they are here for completeness.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.