generic_mutability/genref/
genref_methods.rs

1use super::docs_for;
2use crate::{GenRef, IsMutable, IsShared, Mutability};
3use core::ops::{Deref, DerefMut};
4use core::ptr::NonNull;
5
6mod seal {
7    use crate::{GenRef, Mutability};
8
9    pub trait Sealed {}
10    impl<M: Mutability, T: ?Sized> Sealed for GenRef<'_, M, T> {}
11}
12
13/// This trait allows you to call associated functions of `GenRef` with method syntax.
14///
15/// These functions are not methods on `GenRef` to avoid confusion of own methods with methods of `T` (`GenRef` implements `Deref`).
16/// This trait lets you bypass this restriction in cases where it is not confusing. Use with care.
17///
18/// This trait is only implemented for `GenRef<'_, M, T>` and is sealed so no other types can implement it.
19///
20/// In theory, it is also possible to receive `impl GenRefMethods<'_, M, T>` instead of `GenRef<'_, M, T>` as an argument, which disables the `Deref` impl, although it is more confusing from the caller side.
21pub trait GenRefMethods<'s, M: Mutability, T: ?Sized>: seal::Sealed {
22    /// This is a method variant of the equivalent associated function on `GenRef`.
23    #[doc = docs_for!(as_ptr)]
24    fn as_ptr(&self) -> NonNull<T>;
25
26    /// This is a method variant of the equivalent associated function on `GenRef`.
27    #[doc = docs_for!(gen_into_shared_downgrading)]
28    fn gen_into_shared_downgrading(self) -> &'s T;
29
30    /// This is a method variant of the equivalent associated function on `GenRef`.
31    #[doc = docs_for!(gen_into_mut)]
32    fn gen_into_mut(self, _proof: IsMutable<M>) -> &'s mut T;
33
34    /// This is a method variant of the equivalent associated function on `GenRef`.
35    #[doc = docs_for!(gen_into_shared)]
36    fn gen_into_shared(self, _proof: IsShared<M>) -> &'s T;
37    /// This is a method variant of the equivalent associated function on `GenRef`.
38    #[doc = docs_for!(reborrow)]
39    fn reborrow(&mut self) -> GenRef<'_, M, T>;
40
41    /// This is a method variant of the equivalent associated function on `GenRef`.
42    #[doc = docs_for!(map)]
43    fn map<U: ?Sized>(
44        self,
45        f_mut: impl FnOnce(&mut T) -> &mut U,
46        f_shared: impl FnOnce(&T) -> &U,
47    ) -> GenRef<'s, M, U>;
48
49    /// This is a method variant of the equivalent associated function on `GenRef`.
50    #[doc = docs_for!(map_deref)]
51    fn map_deref(self) -> GenRef<'s, M, T::Target>
52    where
53        T: Deref + DerefMut;
54
55    /// Dereferences the `GenRef`. Same as `Deref::deref(self)`.
56    /// This method allows you to call methods on the referenced value explicitly.
57    fn deref(&self) -> &T;
58}
59impl<'s, M: Mutability, T: ?Sized> GenRefMethods<'s, M, T> for GenRef<'s, M, T> {
60    #[inline(always)]
61    fn as_ptr(&self) -> NonNull<T> {
62        GenRef::as_ptr(self)
63    }
64
65    #[inline(always)]
66    fn gen_into_shared_downgrading(self) -> &'s T {
67        GenRef::gen_into_shared_downgrading(self)
68    }
69    #[inline(always)]
70    fn gen_into_mut(self, proof: IsMutable<M>) -> &'s mut T {
71        GenRef::gen_into_mut(self, proof)
72    }
73
74    #[inline(always)]
75    fn gen_into_shared(self, proof: IsShared<M>) -> &'s T {
76        GenRef::gen_into_shared(self, proof)
77    }
78
79    #[inline(always)]
80    fn reborrow(&mut self) -> GenRef<'_, M, T> {
81        GenRef::reborrow(self)
82    }
83
84    #[inline(always)]
85    fn map<U: ?Sized>(
86        self,
87        f_mut: impl FnOnce(&mut T) -> &mut U,
88        f_shared: impl FnOnce(&T) -> &U,
89    ) -> GenRef<'s, M, U> {
90        GenRef::map(self, f_shared, f_mut)
91    }
92
93    #[inline(always)]
94    fn map_deref(self) -> GenRef<'s, M, T::Target>
95    where
96        T: Deref + DerefMut,
97    {
98        GenRef::map_deref(self)
99    }
100
101    #[inline(always)]
102    fn deref(&self) -> &T {
103        self
104    }
105}