1#![no_std]
91
92#[cfg(feature = "derive")]
96pub use reborrow_derive::{ReborrowCopyTraits, ReborrowTraits};
97
98pub trait Reborrow<'short, _Outlives = &'short Self> {
100 type Target;
101 #[must_use]
102 fn rb(&'short self) -> Self::Target;
103}
104
105pub trait ReborrowMut<'short, _Outlives = &'short Self> {
107 type Target;
108 #[must_use]
109 fn rb_mut(&'short mut self) -> Self::Target;
110}
111
112pub trait IntoConst {
114 type Target;
115 #[must_use]
116 fn into_const(self) -> Self::Target;
117}
118
119pub trait AsGeneralizedRef<'short, Target, _Outlives = &'short Self> {
122 #[must_use]
123 fn as_generalized_ref(&'short self) -> Target;
124}
125
126pub trait AsGeneralizedMut<'short, Target, _Outlives = &'short Self> {
129 #[must_use]
130 fn as_generalized_mut(&'short mut self) -> Target;
131}
132
133impl<'short, T: ?Sized + AsRef<Target>, Target: ?Sized> AsGeneralizedRef<'short, &'short Target>
134 for T
135{
136 #[inline]
137 fn as_generalized_ref(&'short self) -> &'short Target {
138 self.as_ref()
139 }
140}
141
142impl<'short, T: ?Sized + AsMut<Target>, Target: ?Sized> AsGeneralizedMut<'short, &'short mut Target>
143 for T
144{
145 #[inline]
146 fn as_generalized_mut(&'short mut self) -> &'short mut Target {
147 self.as_mut()
148 }
149}
150
151impl<'short, 'a, T> Reborrow<'short> for &'a T
152where
153 T: ?Sized,
154{
155 type Target = &'short T;
156
157 #[inline]
158 fn rb(&'short self) -> Self::Target {
159 *self
160 }
161}
162
163impl<'short, 'a, T> ReborrowMut<'short> for &'a T
164where
165 T: ?Sized,
166{
167 type Target = &'short T;
168
169 #[inline]
170 fn rb_mut(&'short mut self) -> Self::Target {
171 *self
172 }
173}
174
175impl<'a, T> IntoConst for &'a T
176where
177 T: ?Sized,
178{
179 type Target = &'a T;
180
181 #[inline]
182 fn into_const(self) -> Self::Target {
183 self
184 }
185}
186
187impl<'short, 'a, T> Reborrow<'short> for &'a mut T
188where
189 T: ?Sized,
190{
191 type Target = &'short T;
192
193 #[inline]
194 fn rb(&'short self) -> Self::Target {
195 *self
196 }
197}
198
199impl<'short, 'a, T> ReborrowMut<'short> for &'a mut T
200where
201 T: ?Sized,
202{
203 type Target = &'short mut T;
204
205 #[inline]
206 fn rb_mut(&'short mut self) -> Self::Target {
207 *self
208 }
209}
210
211impl<'a, T> IntoConst for &'a mut T
212where
213 T: ?Sized,
214{
215 type Target = &'a T;
216
217 #[inline]
218 fn into_const(self) -> Self::Target {
219 self
220 }
221}
222
223impl<'short, T> Reborrow<'short> for Option<T>
224where
225 T: Reborrow<'short>,
226{
227 type Target = Option<T::Target>;
228
229 #[inline]
230 fn rb(&'short self) -> Self::Target {
231 match self {
232 &None => None,
233 &Some(ref x) => Some(x.rb()),
234 }
235 }
236}
237
238impl<'short, T> ReborrowMut<'short> for Option<T>
239where
240 T: ReborrowMut<'short>,
241{
242 type Target = Option<T::Target>;
243
244 #[inline]
245 fn rb_mut(&'short mut self) -> Self::Target {
246 match self {
247 &mut None => None,
248 &mut Some(ref mut x) => Some(x.rb_mut()),
249 }
250 }
251}
252
253impl<T> IntoConst for Option<T>
254where
255 T: IntoConst,
256{
257 type Target = Option<T::Target>;
258
259 #[inline]
260 fn into_const(self) -> Self::Target {
261 match self {
262 None => None,
263 Some(x) => Some(x.into_const()),
264 }
265 }
266}
267
268impl<'short, T: AsGeneralizedRef<'short, Target>, Target> AsGeneralizedRef<'short, Option<Target>>
269 for Option<T>
270{
271 #[inline]
272 fn as_generalized_ref(&'short self) -> Option<Target> {
273 match self {
274 None => None,
275 &Some(ref x) => Some(x.as_generalized_ref()),
276 }
277 }
278}
279
280impl<'short, T: AsGeneralizedMut<'short, Target>, Target> AsGeneralizedMut<'short, Option<Target>>
281 for Option<T>
282{
283 #[inline]
284 fn as_generalized_mut(&'short mut self) -> Option<Target> {
285 match self {
286 None => None,
287 &mut Some(ref mut x) => Some(x.as_generalized_mut()),
288 }
289 }
290}
291
292#[cfg(test)]
293mod tests {
294 use super::*;
295
296 #[test]
297 fn option() {
298 let mut a = 0;
299 let mut opt = Some(&mut a);
300 let opt_mut = &mut opt;
301 let _ = opt_mut.rb_mut();
302 }
303
304 #[test]
305 fn custom_view_type() {
306 struct MyViewType<'a> {
307 r: &'a mut i32,
308 }
309
310 impl<'short, 'a> ReborrowMut<'short> for MyViewType<'a> {
311 type Target = MyViewType<'short>;
312
313 fn rb_mut(&'short mut self) -> Self::Target {
314 MyViewType { r: self.r }
315 }
316 }
317
318 fn takes_mut_option(_o: Option<MyViewType>) {}
319
320 let mut x = 0;
321 let mut o = Some(MyViewType { r: &mut x });
322 takes_mut_option(o.rb_mut());
323 takes_mut_option(o.rb_mut());
324 drop(o);
325 }
326
327 #[test]
328 fn as_ref() {
329 let v = [()];
330 let _r: &[()] = v.as_generalized_ref();
331 }
332}