/rust/registry/src/index.crates.io-1949cf8c6b5b557f/yoke-0.8.2/src/macro_impls.rs
Line | Count | Source |
1 | | // This file is part of ICU4X. For terms of use, please see the file |
2 | | // called LICENSE at the top level of the ICU4X source tree |
3 | | // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
4 | | |
5 | | // In this case consistency between impls is more important |
6 | | // than using pointer casts |
7 | | #![allow(clippy::transmute_ptr_to_ptr)] |
8 | | |
9 | | use crate::Yokeable; |
10 | | use core::{mem::ManuallyDrop, ptr}; |
11 | | |
12 | | macro_rules! copy_yoke_impl { |
13 | | () => { |
14 | | #[inline] |
15 | 0 | fn transform(&self) -> &Self::Output { |
16 | 0 | self |
17 | 0 | } Unexecuted instantiation: <() as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <u8 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <u16 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <u32 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <u64 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <u128 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <usize as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <i8 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <i16 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <i32 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <i64 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <i128 as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <isize as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <char as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <bool as yoke::yokeable::Yokeable>::transform |
18 | | #[inline] |
19 | 0 | fn transform_owned(self) -> Self::Output { |
20 | 0 | self |
21 | 0 | } Unexecuted instantiation: <() as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <u8 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <u16 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <u32 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <u64 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <u128 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <usize as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <i8 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <i16 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <i32 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <i64 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <i128 as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <isize as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <char as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <bool as yoke::yokeable::Yokeable>::transform_owned |
22 | | #[inline] |
23 | 0 | unsafe fn make(this: Self::Output) -> Self { |
24 | 0 | this |
25 | 0 | } Unexecuted instantiation: <() as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <u8 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <u16 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <u32 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <u64 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <u128 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <usize as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <i8 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <i16 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <i32 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <i64 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <i128 as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <isize as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <char as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <bool as yoke::yokeable::Yokeable>::make |
26 | | #[inline] |
27 | 0 | fn transform_mut<F>(&'a mut self, f: F) |
28 | 0 | where |
29 | 0 | F: 'static + for<'b> FnOnce(&'b mut Self::Output), |
30 | | { |
31 | 0 | f(self) |
32 | 0 | } Unexecuted instantiation: <() as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <u8 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <u16 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <u32 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <u64 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <u128 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <usize as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <i8 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <i16 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <i32 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <i64 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <i128 as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <isize as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <char as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <bool as yoke::yokeable::Yokeable>::transform_mut::<_> |
33 | | }; |
34 | | } |
35 | | macro_rules! impl_copy_type { |
36 | | ($ty:ty) => { |
37 | | // Safety: all the types that this macro is used to generate impls of Yokeable for do not |
38 | | // borrow any memory. |
39 | | unsafe impl<'a> Yokeable<'a> for $ty { |
40 | | type Output = Self; |
41 | | copy_yoke_impl!(); |
42 | | } |
43 | | }; |
44 | | } |
45 | | |
46 | | impl_copy_type!(()); |
47 | | impl_copy_type!(u8); |
48 | | impl_copy_type!(u16); |
49 | | impl_copy_type!(u32); |
50 | | impl_copy_type!(u64); |
51 | | impl_copy_type!(u128); |
52 | | impl_copy_type!(usize); |
53 | | impl_copy_type!(i8); |
54 | | impl_copy_type!(i16); |
55 | | impl_copy_type!(i32); |
56 | | impl_copy_type!(i64); |
57 | | impl_copy_type!(i128); |
58 | | impl_copy_type!(isize); |
59 | | impl_copy_type!(char); |
60 | | impl_copy_type!(bool); |
61 | | |
62 | | // This is for when we're implementing Yoke on a complex type such that it's not |
63 | | // obvious to the compiler that the lifetime is covariant |
64 | | // |
65 | | // Safety: the caller of this macro must ensure that `Self` is indeed covariant in 'a. |
66 | | macro_rules! unsafe_complex_yoke_impl { |
67 | | () => { |
68 | 0 | fn transform(&'a self) -> &'a Self::Output { |
69 | | // Safety: equivalent to casting the lifetime. Macro caller ensures covariance. |
70 | 0 | unsafe { &*(self as *const Self as *const Self::Output) } |
71 | 0 | } Unexecuted instantiation: <core::option::Option<_> as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <[_; _] as yoke::yokeable::Yokeable>::transform Unexecuted instantiation: <(_, _) as yoke::yokeable::Yokeable>::transform |
72 | | |
73 | 0 | fn transform_owned(self) -> Self::Output { |
74 | | // Safety: equivalent to casting the lifetime. Macro caller ensures covariance. |
75 | | unsafe { |
76 | 0 | let ptr: *const Self::Output = (&self as *const Self).cast(); |
77 | 0 | let _ = ManuallyDrop::new(self); |
78 | 0 | ptr::read(ptr) |
79 | | } |
80 | 0 | } Unexecuted instantiation: <core::option::Option<_> as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <[_; _] as yoke::yokeable::Yokeable>::transform_owned Unexecuted instantiation: <(_, _) as yoke::yokeable::Yokeable>::transform_owned |
81 | | |
82 | 0 | unsafe fn make(from: Self::Output) -> Self { |
83 | 0 | debug_assert!(size_of::<Self::Output>() == size_of::<Self>()); |
84 | 0 | let ptr = &from as *const Self::Output as *const Self; |
85 | 0 | let _ = ManuallyDrop::new(from); |
86 | | // Safety: `ptr` is certainly valid, aligned and points to a properly initialized value, as |
87 | | // it comes from a value that was moved into a ManuallyDrop. |
88 | 0 | unsafe { ptr::read(ptr) } |
89 | 0 | } Unexecuted instantiation: <core::option::Option<_> as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <[_; _] as yoke::yokeable::Yokeable>::make Unexecuted instantiation: <(_, _) as yoke::yokeable::Yokeable>::make |
90 | | |
91 | 0 | fn transform_mut<F>(&'a mut self, f: F) |
92 | 0 | where |
93 | 0 | F: 'static + for<'b> FnOnce(&'b mut Self::Output), |
94 | | { |
95 | 0 | crate::utils::transform_mut_yokeable(self, f) |
96 | 0 | } Unexecuted instantiation: <core::option::Option<_> as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <[_; _] as yoke::yokeable::Yokeable>::transform_mut::<_> Unexecuted instantiation: <(_, _) as yoke::yokeable::Yokeable>::transform_mut::<_> |
97 | | }; |
98 | | } |
99 | | |
100 | | // Safety: since T implements Yokeable<'a>, Option<T<'b>> must be covariant on 'b or the Yokeable |
101 | | // implementation on T would be unsound. |
102 | | unsafe impl<'a, T: 'static + for<'b> Yokeable<'b>> Yokeable<'a> for Option<T> { |
103 | | type Output = Option<<T as Yokeable<'a>>::Output>; |
104 | | unsafe_complex_yoke_impl!(); |
105 | | } |
106 | | |
107 | | // Safety: since T1, T2 implement Yokeable<'a>, (T1<'b>, T2<'b>) must be covariant on 'b or the Yokeable |
108 | | // implementation on T would be unsound. |
109 | | unsafe impl<'a, T1: 'static + for<'b> Yokeable<'b>, T2: 'static + for<'b> Yokeable<'b>> Yokeable<'a> |
110 | | for (T1, T2) |
111 | | { |
112 | | type Output = (<T1 as Yokeable<'a>>::Output, <T2 as Yokeable<'a>>::Output); |
113 | | unsafe_complex_yoke_impl!(); |
114 | | } |
115 | | |
116 | | // Safety: since T implements Yokeable<'a>, [T<'b>; N] must be covariant on 'b or the Yokeable |
117 | | // implementation on T would be unsound. |
118 | | unsafe impl<'a, T: 'static + for<'b> Yokeable<'b>, const N: usize> Yokeable<'a> for [T; N] { |
119 | | type Output = [<T as Yokeable<'a>>::Output; N]; |
120 | | unsafe_complex_yoke_impl!(); |
121 | | } |