Debian Codegraph @

debian/missing-sources/derive_more/src/as.rs

raw
1 //! Type glue for [autoref-based specialization][0], used in [`AsRef`]/[`AsMut`] macro expansion.
2 //!
3 //! Allows tp specialize the `impl<T> AsRef<T> for T` case over the default
4 //! `impl<Inner: AsRef<B>, B> AsRef<B> for Outer<Inner>` one.
5 //!
6 //! [0]: https://lukaskalbertodt.github.io/2019/12/05/generalized-autoref-based-specialization.html
7
8 use core::marker::PhantomData;
9
10 /// Container to specialize over.
11 pub struct Conv<Frm: ?Sized, To: ?Sized>(PhantomData<(*const Frm, *const To)>);
12
13 impl<Frm: ?Sized, To: ?Sized> Default for Conv<Frm, To> {
14 fn default() -> Self {
15 Self(PhantomData)
16 }
17 }
18
19 /// Trait performing the specialization.
20 pub trait ExtractRef {
21 /// Input reference type.
22 type Frm;
23 /// Output reference type.
24 type To;
25
26 /// Extracts the output type from the input one.
27 fn __extract_ref(&self, frm: Self::Frm) -> Self::To;
28 }
29
30 impl<'a, T> ExtractRef for &Conv<&'a T, T>
31 where
32 T: ?Sized,
33 {
34 type Frm = &'a T;
35 type To = &'a T;
36
37 fn __extract_ref(&self, frm: Self::Frm) -> Self::To {
38 frm
39 }
40 }
41
42 impl<'a, Frm, To> ExtractRef for Conv<&'a Frm, To>
43 where
44 Frm: AsRef<To> + ?Sized,
45 To: ?Sized + 'a,
46 {
47 type Frm = &'a Frm;
48 type To = &'a To;
49
50 fn __extract_ref(&self, frm: Self::Frm) -> Self::To {
51 frm.as_ref()
52 }
53 }
54
55 impl<'a, T> ExtractRef for &Conv<&'a mut T, T>
56 where
57 T: ?Sized,
58 {
59 type Frm = &'a mut T;
60 type To = &'a mut T;
61
62 fn __extract_ref(&self, frm: Self::Frm) -> Self::To {
63 frm
64 }
65 }
66
67 impl<'a, Frm, To> ExtractRef for Conv<&'a mut Frm, To>
68 where
69 Frm: AsMut<To> + ?Sized,
70 To: ?Sized + 'a,
71 {
72 type Frm = &'a mut Frm;
73 type To = &'a mut To;
74
75 fn __extract_ref(&self, frm: Self::Frm) -> Self::To {
76 frm.as_mut()
77 }
78 }