1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Workarounds for adding trait bounds to `yoke` objects.
//!
//! # Trait bounds in Yoke
//!
//! [Compiler bug #89196](https://github.com/rust-lang/rust/issues/89196) makes it tricky to add
//! trait bounds involving `yoke` types.
//!
//! For example, you may want to write:
//!
//! `where for<'a> <Y as Yokeable<'a>>::Output: MyTrait`
//!
//! The above trait bound will compile, but at call sites, you get errors such as:
//!
//! > the trait `for<'de> MyTrait` is not implemented for `<Y as Yokeable<'de>>::Output`
//!
//! There are two known workarounds:
//!
//! 1. If the trait is well-defined on references, like `Debug`, bind the trait to a reference:
//!     `where for<'a> &'a <Y as Yokeable<'a>>::Output: MyTrait`
//! 2. If the trait involves `Self`, like `Clone`, use [`YokeTraitHack`]:
//!     `where for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: MyTrait`
//!
//! # Examples
//!
//! Code that does not compile ([playground](https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=ebbda5b15a398d648bdff9e439b27dc0)):
//!
//! ```compile_fail
//! # this compiles in 1.78+, so this text will make it fail
//! use yoke::*;
//!
//! trait MiniDataMarker {
//!     type Yokeable: for<'a> Yokeable<'a>;
//! }
//!
//! struct MiniDataPayload<M>
//! where
//!     M: MiniDataMarker
//! {
//!     pub yoke: Yoke<M::Yokeable, ()>,
//! }
//!
//! impl<M> Clone for MiniDataPayload<M>
//! where
//!     M: MiniDataMarker,
//!     for<'a> <M::Yokeable as Yokeable<'a>>::Output: Clone,
//! {
//!     fn clone(&self) -> Self {
//!         unimplemented!()
//!     }
//! }
//!
//! trait MiniDataProvider<M>
//! where
//!     M: MiniDataMarker
//! {
//!     fn mini_load_data(&self) -> MiniDataPayload<M>;
//! }
//!
//! struct MiniStructProvider<M>
//! where
//!     M: MiniDataMarker,
//! {
//!     pub payload: MiniDataPayload<M>,
//! }
//!
//! impl<M> MiniDataProvider<M> for MiniStructProvider<M>
//! where
//!     M: MiniDataMarker,
//!     for<'a> <M::Yokeable as Yokeable<'a>>::Output: Clone,
//! {
//!     fn mini_load_data(&self) -> MiniDataPayload<M> {
//!         self.payload.clone()
//!     }
//! }
//!
//! #[derive(Clone)]
//! struct SimpleStruct(pub u32);
//!
//! unsafe impl<'a> Yokeable<'a> for SimpleStruct {
//!     // (not shown; see `Yokeable` for examples)
//! #    type Output = SimpleStruct;
//! #    fn transform(&'a self) -> &'a Self::Output {
//! #        self
//! #    }
//! #    fn transform_owned(self) -> Self::Output {
//! #        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,
//! #            ))
//! #        }
//! #    }
//! }
//!
//! impl MiniDataMarker for SimpleStruct {
//!     type Yokeable = SimpleStruct;
//! }
//!
//! let provider = MiniStructProvider {
//!     payload: MiniDataPayload {
//!         yoke: Yoke::new_always_owned(SimpleStruct(42))
//!     }
//! };
//!
//! // Broken:
//! // "method cannot be called on `MiniStructProvider<_>` due to unsatisfied trait bounds"
//! let payload: MiniDataPayload<SimpleStruct> = provider.mini_load_data();
//!
//! // Working:
//! let payload = MiniDataProvider::<SimpleStruct>::mini_load_data(&provider);
//!
//! assert_eq!(payload.yoke.get().0, 42);
//! ```
//!
//! Example for binding the trait to a reference:
//!
//! ```
//! use yoke::Yoke;
//! use yoke::Yokeable;
//!
//! // Example trait and struct for illustration purposes:
//! trait MyTrait {
//!     fn demo(&self) -> u32;
//! }
//! struct MyStruct(u32);
//! impl MyTrait for MyStruct {
//!     fn demo(&self) -> u32 {
//!         self.0
//!     }
//! }
//! unsafe impl<'a> Yokeable<'a> for MyStruct {
//!     // (not shown; see `Yokeable` for examples)
//! #    type Output = MyStruct;
//! #    fn transform(&'a self) -> &'a Self::Output {
//! #        self
//! #    }
//! #    fn transform_owned(self) -> Self::Output {
//! #        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,
//! #            ))
//! #        }
//! #    }
//! }
//!
//! // The trait needs to be defined on references:
//! impl<'a, T> MyTrait for &'a T
//! where
//!     T: MyTrait,
//! {
//!     fn demo(&self) -> u32 {
//!         self.demo()
//!     }
//! }
//!
//! impl<Y, C> MyTrait for Yoke<Y, C>
//! where
//!     Y: for<'a> Yokeable<'a>,
//!     for<'a> &'a <Y as Yokeable<'a>>::Output: MyTrait,
//! {
//!     fn demo(&self) -> u32 {
//!         self.get().demo()
//!     }
//! }
//!
//! fn example() {
//!     let y = Yoke::<MyStruct, ()>::new_always_owned(MyStruct(42));
//!     let _: &dyn MyTrait = &y;
//! }
//! ```
//!
//! Example for using [`YokeTraitHack`]:
//!
//! ```
//! use std::rc::Rc;
//! use yoke::trait_hack::YokeTraitHack;
//! use yoke::Yoke;
//! use yoke::Yokeable;
//!
//! // Example trait and struct for illustration purposes:
//! trait MyTrait {
//!     fn demo(data: u32) -> Self;
//! }
//! struct MyStruct(u32);
//! impl MyTrait for MyStruct {
//!     fn demo(data: u32) -> Self {
//!         Self(data)
//!     }
//! }
//! unsafe impl<'a> Yokeable<'a> for MyStruct {
//!     // (not shown; see `Yokeable` for examples)
//! #    type Output = MyStruct;
//! #    fn transform(&'a self) -> &'a Self::Output {
//! #        self
//! #    }
//! #    fn transform_owned(self) -> Self::Output {
//! #        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,
//! #            ))
//! #        }
//! #    }
//! }
//!
//! // The trait needs to be defined on YokeTraitHack:
//! impl<'a, T> MyTrait for YokeTraitHack<T>
//! where
//!     T: MyTrait,
//! {
//!     fn demo(data: u32) -> Self {
//!         YokeTraitHack(T::demo(data))
//!     }
//! }
//!
//! impl<Y> MyTrait for Yoke<Y, Rc<u32>>
//! where
//!     Y: for<'a> Yokeable<'a>,
//!     for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: MyTrait,
//! {
//!     fn demo(data: u32) -> Self {
//!         let rc_u32: Rc<u32> = Rc::new(data);
//!         Yoke::attach_to_cart(rc_u32, |u| {
//!             YokeTraitHack::<<Y as Yokeable>::Output>::demo(*u).0
//!         })
//!     }
//! }
//!
//! fn example() {
//!     let _ = Yoke::<MyStruct, Rc<u32>>::demo(42);
//! }
//! ```

use core::mem;

/// A wrapper around a type `T`, forwarding trait calls down to the inner type.
///
/// `YokeTraitHack` supports [`Clone`], [`PartialEq`], [`Eq`], and [`serde::Deserialize`] out of
/// the box. Other traits can be implemented by the caller.
///
/// For more information, see the module-level documentation.
///
/// # Example
///
/// Using `YokeTraitHack` as a type bound in a function comparing two `Yoke`s:
///
/// ```
/// use yoke::trait_hack::YokeTraitHack;
/// use yoke::*;
///
/// fn compare_yokes<Y, C1, C2>(y1: Yoke<Y, C1>, y2: Yoke<Y, C2>) -> bool
/// where
///     Y: for<'a> Yokeable<'a>,
///     for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: PartialEq,
/// {
///     YokeTraitHack(y1.get()).into_ref() == YokeTraitHack(y2.get()).into_ref()
/// }
/// ```
#[repr(transparent)]
#[derive(Clone, PartialEq, Eq, Debug)]
#[allow(clippy::exhaustive_structs)] // newtype
pub struct YokeTraitHack<T>(pub T);

impl<'a, T> YokeTraitHack<&'a T> {
    /// Converts from `YokeTraitHack<&T>` to `&YokeTraitHack<T>`.
    ///
    /// This is safe because `YokeTraitHack` is `repr(transparent)`.
    ///
    /// This method is required to implement `Clone` on `Yoke`.
    pub fn into_ref(self) -> &'a YokeTraitHack<T> {
        // YokeTraitHack is repr(transparent) so it's always safe
        // to transmute YTH<&T> to &YTH<T>
        unsafe { mem::transmute::<YokeTraitHack<&T>, &YokeTraitHack<T>>(self) }
    }
}

// This is implemented manually to avoid the serde derive dependency.
#[cfg(feature = "serde")]
impl<'de, T> serde::de::Deserialize<'de> for YokeTraitHack<T>
where
    T: serde::de::Deserialize<'de>,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        T::deserialize(deserializer).map(YokeTraitHack)
    }
}