generic_mutability/genref/
impl_traits.rs

1use core::cmp::Ordering;
2use core::fmt;
3use core::hash::Hash;
4use core::iter::{DoubleEndedIterator, FusedIterator, Iterator};
5
6#[cfg(any(feature = "std", doc))]
7extern crate std;
8
9#[allow(unused_imports)]
10use crate::{GenRef, Mutability, Mutable, Shared};
11
12impl<M: Mutability, T: ?Sized> Hash for GenRef<'_, M, T>
13where
14    T: Hash,
15{
16    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
17        Hash::hash(&**self, state)
18    }
19}
20
21macro_rules! impl_fmt_traits {
22    ($($trait:ident),+) => {
23        $(
24            impl<'s, M: Mutability, T: ?Sized> fmt::$trait for GenRef<'s, M, T>
25                where T: fmt::$trait
26            {
27                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28                    T::fmt(&**self, f)
29                }
30            }
31        )+
32    };
33}
34impl_fmt_traits!(Debug, Display, LowerExp, UpperExp, Binary, Octal, LowerHex, UpperHex);
35
36macro_rules! impl_partial_eq_ord_for_refs {
37    ($rhs_ty: ident) => {
38        impl<MT: Mutability, T: ?Sized, U: ?Sized> PartialEq<$rhs_ty<'_, U>> for GenRef<'_, MT, T>
39        where
40            T: PartialEq<U>,
41        {
42            fn eq(&self, other: &$rhs_ty<'_, U>) -> bool {
43                T::eq(&**self, &**other)
44            }
45        }
46
47        impl<MT: Mutability, T: ?Sized, U: ?Sized> PartialOrd<$rhs_ty<'_, U>> for GenRef<'_, MT, T>
48        where
49            T: PartialOrd<U>,
50        {
51            fn partial_cmp(&self, other: &$rhs_ty<'_, U>) -> Option<Ordering> {
52                T::partial_cmp(&**self, &**other)
53            }
54        }
55    };
56}
57
58type Ref<'s, T> = &'s T;
59impl_partial_eq_ord_for_refs!(Ref);
60type MutRef<'s, T> = &'s mut T;
61impl_partial_eq_ord_for_refs!(MutRef);
62
63impl<MT: Mutability, MU: Mutability, T: ?Sized, U: ?Sized> PartialEq<GenRef<'_, MU, U>>
64    for GenRef<'_, MT, T>
65where
66    T: PartialEq<U>,
67{
68    fn eq(&self, other: &GenRef<'_, MU, U>) -> bool {
69        T::eq(&**self, &**other)
70    }
71}
72
73impl<M: Mutability, T: ?Sized> Eq for GenRef<'_, M, T> where T: Eq {}
74
75impl<MT: Mutability, MU: Mutability, T: ?Sized, U: ?Sized> PartialOrd<GenRef<'_, MU, U>>
76    for GenRef<'_, MT, T>
77where
78    T: PartialOrd<U>,
79{
80    fn partial_cmp(&self, other: &GenRef<'_, MU, U>) -> Option<Ordering> {
81        T::partial_cmp(&**self, &**other)
82    }
83}
84
85impl<MT: Mutability, T: ?Sized> Ord for GenRef<'_, MT, T>
86where
87    T: Ord,
88{
89    fn cmp(&self, other: &Self) -> Ordering {
90        T::cmp(&**self, &**other)
91    }
92}
93
94#[cfg(any(feature = "std", doc))]
95/// This is only implemented for `GenRef<'_, Shared, T>`, and is not available in a generic context.
96///
97/// This is only available with the feature flag `std`.
98impl<T: ?Sized> std::net::ToSocketAddrs for GenRef<'_, Shared, T>
99where
100    T: std::net::ToSocketAddrs,
101{
102    type Iter = T::Iter;
103    fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
104        std::net::ToSocketAddrs::to_socket_addrs(&**self)
105    }
106}
107
108/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
109impl<T: ?Sized> fmt::Write for GenRef<'_, Mutable, T>
110where
111    T: fmt::Write,
112{
113    fn write_str(&mut self, s: &str) -> fmt::Result {
114        T::write_str(&mut **self, s)
115    }
116
117    fn write_char(&mut self, c: char) -> fmt::Result {
118        T::write_char(&mut **self, c)
119    }
120
121    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
122        T::write_fmt(&mut **self, args)
123    }
124}
125
126/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
127impl<T: ?Sized> Iterator for GenRef<'_, Mutable, T>
128where
129    T: Iterator,
130{
131    type Item = T::Item;
132    fn next(&mut self) -> Option<Self::Item> {
133        T::next(&mut **self)
134    }
135
136    fn nth(&mut self, n: usize) -> Option<Self::Item> {
137        T::nth(&mut **self, n)
138    }
139
140    fn size_hint(&self) -> (usize, Option<usize>) {
141        T::size_hint(&**self)
142    }
143}
144
145/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
146impl<T: ?Sized> DoubleEndedIterator for GenRef<'_, Mutable, T>
147where
148    T: DoubleEndedIterator,
149{
150    fn next_back(&mut self) -> Option<Self::Item> {
151        T::next_back(&mut **self)
152    }
153}
154
155/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
156impl<T: ?Sized> ExactSizeIterator for GenRef<'_, Mutable, T>
157where
158    T: ExactSizeIterator,
159{
160    fn len(&self) -> usize {
161        T::len(&**self)
162    }
163}
164
165/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
166impl<T: ?Sized> FusedIterator for GenRef<'_, Mutable, T> where T: FusedIterator {}
167
168#[cfg(any(feature = "std", doc))]
169/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
170///
171/// This is only available with the feature flag `std`.
172impl<T: ?Sized> std::io::Write for GenRef<'_, Mutable, T>
173where
174    T: std::io::Write,
175{
176    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
177        T::write(&mut **self, buf)
178    }
179    fn flush(&mut self) -> std::io::Result<()> {
180        T::flush(&mut **self)
181    }
182}
183
184#[cfg(any(feature = "std", doc))]
185/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
186///
187/// This is only available with the feature flag `std`.
188impl<T: ?Sized> std::io::Read for GenRef<'_, Mutable, T>
189where
190    T: std::io::Read,
191{
192    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
193        T::read(&mut **self, buf)
194    }
195}
196
197#[cfg(any(feature = "std", doc))]
198/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
199///
200/// This is only available with the feature flag `std`.
201impl<T: ?Sized> std::io::Seek for GenRef<'_, Mutable, T>
202where
203    T: std::io::Seek,
204{
205    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
206        T::seek(&mut **self, pos)
207    }
208}
209
210#[cfg(any(feature = "std", doc))]
211/// This is only implemented for `GenRef<'_, Mutable, T>`, and is not available in a generic context.
212///
213/// This is only available with the feature flag `std`.
214impl<T: ?Sized> std::io::BufRead for GenRef<'_, Mutable, T>
215where
216    T: std::io::BufRead,
217{
218    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
219        T::fill_buf(&mut **self)
220    }
221    fn consume(&mut self, amt: usize) {
222        T::consume(&mut **self, amt)
223    }
224}