ladata/mem/storage/
direct.rs

1// ladata::mem::unbox
2//
3//! Direct storage doesn't affect its content.
4//
5// API based on https://doc.rust-lang.org/alloc/boxed/struct.Box.html
6
7use core::{cmp, fmt, hash, ops};
8
9/// A no-op pointer type, like a [`Box`] that doesn't affect how `T` is stored.
10///
11/// # Examples
12/// ```
13/// use ladata::all::Direct;
14///
15/// let byte = Direct::new(0_u8);
16/// ```
17pub struct Direct<T>(pub T);
18
19impl<T> ops::Deref for Direct<T> {
20    type Target = T;
21    #[inline]
22    fn deref(&self) -> &T {
23        &self.0
24    }
25}
26
27impl<T> Direct<T> {
28    #[inline]
29    pub const fn new(t: T) -> Self {
30        Direct(t)
31    }
32}
33
34impl<T> ops::DerefMut for Direct<T> {
35    #[inline]
36    fn deref_mut(&mut self) -> &mut T {
37        &mut self.0
38    }
39}
40
41impl<T> From<T> for Direct<T> {
42    #[inline]
43    fn from(t: T) -> Self {
44        Direct(t)
45    }
46}
47
48impl<T: Clone> Clone for Direct<T> {
49    #[inline]
50    fn clone(&self) -> Self {
51        Direct(self.0.clone())
52    }
53}
54impl<T: Copy> Copy for Direct<T> {}
55
56impl<T: Default> Default for Direct<T> {
57    #[inline]
58    fn default() -> Self {
59        Direct(T::default())
60    }
61}
62
63impl<T: PartialEq> PartialEq for Direct<T> {
64    #[inline]
65    fn eq(&self, other: &Self) -> bool {
66        self.0.eq(&other.0)
67        // PartialEq::eq(&**self, &**other)
68    }
69}
70impl<T: Eq> Eq for Direct<T> {}
71
72impl<T: PartialOrd> PartialOrd for Direct<T> {
73    #[inline]
74    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
75        self.0.partial_cmp(&other.0)
76        // PartialOrd::partial_cmp(&**self, &**other)
77    }
78}
79impl<T: Ord> Ord for Direct<T> {
80    #[inline]
81    fn cmp(&self, other: &Self) -> cmp::Ordering {
82        self.0.cmp(&other.0)
83        // Ord::cmp(&**self, &**other)
84    }
85}
86
87impl<T: fmt::Debug> fmt::Debug for Direct<T> {
88    #[inline]
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        // fmt::Debug::fmt(&**self, f)
91        fmt::Debug::fmt(&self.0, f)
92    }
93}
94impl<T: fmt::Display> fmt::Display for Direct<T> {
95    #[inline]
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        // fmt::Display::fmt(&**self, f)
98        fmt::Display::fmt(&self.0, f)
99    }
100}
101
102impl<T: hash::Hash> hash::Hash for Direct<T> {
103    #[inline]
104    fn hash<H: hash::Hasher>(&self, state: &mut H) {
105        // (**self).hash(state);
106        self.0.hash(state);
107    }
108}
109impl<T: hash::Hasher> hash::Hasher for Direct<T> {
110    #[inline]
111    fn finish(&self) -> u64 {
112        // (**self).finish()
113        self.0.finish()
114    }
115    #[inline]
116    fn write(&mut self, bytes: &[u8]) {
117        // (**self).write(bytes)
118        self.0.write(bytes)
119    }
120}
121
122impl<I: Iterator> Iterator for Direct<I> {
123    type Item = I::Item;
124    fn next(&mut self) -> Option<I::Item> {
125        (**self).next()
126    }
127    fn size_hint(&self) -> (usize, Option<usize>) {
128        (**self).size_hint()
129    }
130    fn nth(&mut self, n: usize) -> Option<I::Item> {
131        (**self).nth(n)
132    }
133    // fn last(self) -> Option<I::Item> {
134    //     Direct::last(self)
135    // }
136}