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
//! Reference type stand-ins, to be used with [`Selfie`](crate::Selfie) and
//! [`SelfieMut`](crate::SelfieMut) type declarations.
//!
//! These types are stand-ins that allow to name reference types without naming any associated
//! lifetime. The full type can then be reconstructed with an arbitrary lifetime using the
//! [`RefType`] trait.
//!
//! This is necessary for [`Selfie`](crate::Selfie), as it has to work with a self-referential
//! lifetime, which cannot be explicitly named and has to be reconstructed internally.
//! In essence, this is a roundabout way to achieve Higher-Kinded Polymorphism.
//!
//! This module provides stand-ins for common reference types, but you can create your own by
//! implementing the [`RefType`] trait yourself.
use crate::;
use PhantomData;
use Pin;
/// A trait for reference type stand-ins to be combined with an arbitrary lifetime `'a`, to recreate
/// the full reference type.
///
/// # Example
///
/// Implementing [`RefType`] for a custom referential type, so it can be used in a [`Selfie`]:
///
/// ```
/// use std::pin::Pin;
/// use selfie::refs::RefType;
/// use selfie::Selfie;
///
/// #[derive(Copy, Clone)]
/// struct MyReferentialType<'a>(&'a str);
///
/// struct MyReferentialTypeStandIn;
/// impl<'a> RefType<'a> for MyReferentialTypeStandIn {
/// type Ref = MyReferentialType<'a>;
/// }
///
/// // MyReferentialType can now be used in Selfies!
/// let data = Pin::new("Hello, world!".to_owned());
/// let selfie: Selfie<String, MyReferentialTypeStandIn> = Selfie::new(data, |str| MyReferentialType(&str[0..5]));
///
/// assert_eq!("Hello", selfie.with_referential(|r| *r).0);
/// ```
///
/// Here is a dummy example showing how [`RefType`] stand-ins are used internally:
///
/// ```
/// use selfie::refs::{Ref, RefType};
///
/// // These two type declarations are equivalent
/// const STR_1: &'static str = "Hello, world!";
/// const STR_2: <Ref<str> as RefType<'static>>::Ref = "Hello, world!";
///
/// assert_eq!(STR_1, STR_2);
/// ```
/// A stand-in for a shared reference `&T`.
///
/// # Example
///
/// ```
/// use std::pin::Pin;
/// use selfie::refs::Ref;
/// use selfie::Selfie;
///
/// let data = Pin::new("Hello, world!".to_owned());
/// let selfie: Selfie<String, Ref<str>> = Selfie::new(data, |str| &str[0..5]);
///
/// assert_eq!("Hello", selfie.with_referential(|r| *r));
/// ```
;
/// A stand-in for a mutable reference `&mut T`.
///
/// # Example
///
/// ```
/// use std::pin::Pin;
/// use selfie::refs::Mut;
/// use selfie::SelfieMut;
///
/// let data = Pin::new("Hello, world!".to_owned());
/// let selfie: SelfieMut<String, Mut<str>> =
/// SelfieMut::new(data, |str| &mut Pin::into_inner(str)[0..5]);
///
/// selfie.with_referential(|str| assert_eq!("Hello", *str));
/// ```
;
/// A stand-in for a [`Selfie`](crate::Selfie) holding a reference type as its owned pointer.
///
/// # Example
///
/// ```
/// use std::pin::Pin;
/// use selfie::refs::{Ref, SelfieRef};
/// use selfie::Selfie;
///
/// let data = Pin::new("Hello, world!".to_owned());
/// let selfie: Selfie<String, SelfieRef<Ref<str>, Ref<str>>> = Selfie::new(data, |str| {
/// let substr = Pin::new(&str[0..5]);
/// Selfie::new(substr, |str| &str[3..])
/// });
///
/// assert_eq!("Hello, world!", selfie.owned());
/// selfie.with_referential(|r| {
/// assert_eq!("Hello", r.owned());
/// assert_eq!("lo", r.with_referential(|r| *r));
/// });
/// ```
where
P: ?Sized,
R: ?Sized;
/// A stand-in for a [`SelfieMut`](crate::SelfieMut) holding a reference type as its owned pointer.
///
/// # Example
///
/// ```
/// use std::pin::Pin;
/// use selfie::refs::{Mut, SelfieRefMut};
/// use selfie::SelfieMut;
///
/// let data = Pin::new("Hello, world!".to_owned());
/// let selfie: SelfieMut<String, SelfieRefMut<Mut<str>, Mut<str>>> = SelfieMut::new(data, |str| {
/// let substr = Pin::new(&mut Pin::into_inner(str)[0..5]);
/// SelfieMut::new(substr, |str| &mut Pin::into_inner(str)[3..])
/// });
///
/// selfie.with_referential(|inner_selfie| {
/// inner_selfie.with_referential(|str| assert_eq!("lo", *str))
/// });
/// ```
where
P: ?Sized,
R: ?Sized;
// Other std types