generic_container/
generic_container.rs1#![warn(
2 clippy::missing_inline_in_public_items,
3 reason = "the wrapper type should mostly just delegate",
4)]
5
6use core::{cmp::Ordering, marker::PhantomData};
7use core::{
8 fmt::{Debug, Formatter, Result as FmtResult},
9 hash::{Hash, Hasher},
10};
11
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
14
15
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
69#[repr(transparent)]
70pub struct GenericContainer<T: ?Sized, C: ?Sized> {
71 pub _marker: PhantomData<T>,
73 pub container: C,
77}
78
79impl<T: ?Sized, C> GenericContainer<T, C> {
80 #[inline]
83 #[must_use]
84 pub const fn new(container: C) -> Self {
85 Self {
86 _marker: PhantomData,
87 container,
88 }
89 }
90}
91
92impl<T: ?Sized, C: Default> Default for GenericContainer<T, C> {
93 #[inline]
94 fn default() -> Self {
95 Self::new(C::default())
96 }
97}
98
99impl<T, C> Debug for GenericContainer<T, C>
100where
101 T: ?Sized,
102 C: ?Sized + Debug,
103{
104 #[allow(clippy::missing_inline_in_public_items, reason = "not trivial or likely to be hot")]
105 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
106 f.debug_struct("GenericContainer")
107 .field("_marker", &self._marker)
108 .field("container", &&self.container)
109 .finish()
110 }
111}
112
113impl<T: ?Sized, C: Copy> Copy for GenericContainer<T, C> {}
114
115impl<T: ?Sized, C: Clone> Clone for GenericContainer<T, C> {
116 #[inline]
117 fn clone(&self) -> Self {
118 Self {
119 _marker: self._marker,
120 container: self.container.clone(),
121 }
122 }
123
124 #[inline]
125 fn clone_from(&mut self, source: &Self) {
126 self.container.clone_from(&source.container);
127 }
128}
129
130impl<T: ?Sized, C: ?Sized + PartialEq<C>> PartialEq<Self> for GenericContainer<T, C> {
131 #[inline]
132 fn eq(&self, other: &Self) -> bool {
133 self.container.eq(&other.container)
134 }
135}
136
137impl<T: ?Sized, C: ?Sized + Eq> Eq for GenericContainer<T, C> {}
138
139impl<T: ?Sized, C: ?Sized + PartialOrd<C>> PartialOrd<Self> for GenericContainer<T, C> {
140 #[inline]
141 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
142 self.container.partial_cmp(&other.container)
143 }
144}
145
146impl<T: ?Sized, C: ?Sized + Ord> Ord for GenericContainer<T, C> {
147 #[inline]
148 fn cmp(&self, other: &Self) -> Ordering {
149 self.container.cmp(&other.container)
150 }
151}
152
153impl<T: ?Sized, C: ?Sized + Hash> Hash for GenericContainer<T, C> {
154 #[inline]
155 fn hash<H: Hasher>(&self, state: &mut H) {
156 self.container.hash(state);
157 }
158}