bitvec/boxed/
traits.rs

1//! General trait implementations for boxed bit-slices.
2
3use alloc::{
4	borrow::Cow,
5	boxed::Box,
6};
7use core::{
8	borrow::{
9		Borrow,
10		BorrowMut,
11	},
12	cmp,
13	convert::TryFrom,
14	fmt::{
15		self,
16		Debug,
17		Display,
18		Formatter,
19	},
20	hash::{
21		Hash,
22		Hasher,
23	},
24	iter::FromIterator,
25};
26
27use tap::Pipe;
28
29use super::BitBox;
30use crate::{
31	array::BitArray,
32	order::BitOrder,
33	slice::BitSlice,
34	store::BitStore,
35	vec::BitVec,
36	view::BitViewSized,
37};
38
39#[cfg(not(tarpaulin_include))]
40impl<T, O> Borrow<BitSlice<T, O>> for BitBox<T, O>
41where
42	T: BitStore,
43	O: BitOrder,
44{
45	#[inline]
46	fn borrow(&self) -> &BitSlice<T, O> {
47		self.as_bitslice()
48	}
49}
50
51#[cfg(not(tarpaulin_include))]
52impl<T, O> BorrowMut<BitSlice<T, O>> for BitBox<T, O>
53where
54	T: BitStore,
55	O: BitOrder,
56{
57	#[inline]
58	fn borrow_mut(&mut self) -> &mut BitSlice<T, O> {
59		self.as_mut_bitslice()
60	}
61}
62
63#[cfg(not(tarpaulin_include))]
64impl<T, O> Clone for BitBox<T, O>
65where
66	T: BitStore,
67	O: BitOrder,
68{
69	#[inline]
70	fn clone(&self) -> Self {
71		self.as_bitslice().pipe(Self::from_bitslice)
72	}
73}
74
75#[cfg(not(tarpaulin_include))]
76impl<T, O> Eq for BitBox<T, O>
77where
78	T: BitStore,
79	O: BitOrder,
80{
81}
82
83#[cfg(not(tarpaulin_include))]
84impl<T, O> Ord for BitBox<T, O>
85where
86	T: BitStore,
87	O: BitOrder,
88{
89	#[inline]
90	fn cmp(&self, other: &Self) -> cmp::Ordering {
91		self.as_bitslice().cmp(other.as_bitslice())
92	}
93}
94
95#[cfg(not(tarpaulin_include))]
96impl<O1, O2, T1, T2> PartialEq<BitBox<T2, O2>> for BitSlice<T1, O1>
97where
98	O1: BitOrder,
99	O2: BitOrder,
100	T1: BitStore,
101	T2: BitStore,
102{
103	#[inline]
104	fn eq(&self, other: &BitBox<T2, O2>) -> bool {
105		self == other.as_bitslice()
106	}
107}
108
109#[cfg(not(tarpaulin_include))]
110impl<O1, O2, T1, T2> PartialEq<BitBox<T2, O2>> for &BitSlice<T1, O1>
111where
112	O1: BitOrder,
113	O2: BitOrder,
114	T1: BitStore,
115	T2: BitStore,
116{
117	#[inline]
118	fn eq(&self, other: &BitBox<T2, O2>) -> bool {
119		*self == other.as_bitslice()
120	}
121}
122
123#[cfg(not(tarpaulin_include))]
124impl<O1, O2, T1, T2> PartialEq<BitBox<T2, O2>> for &mut BitSlice<T1, O1>
125where
126	O1: BitOrder,
127	O2: BitOrder,
128	T1: BitStore,
129	T2: BitStore,
130{
131	#[inline]
132	fn eq(&self, other: &BitBox<T2, O2>) -> bool {
133		**self == other.as_bitslice()
134	}
135}
136
137#[cfg(not(tarpaulin_include))]
138impl<T, O, Rhs> PartialEq<Rhs> for BitBox<T, O>
139where
140	T: BitStore,
141	O: BitOrder,
142	Rhs: ?Sized + PartialEq<BitSlice<T, O>>,
143{
144	#[inline]
145	fn eq(&self, other: &Rhs) -> bool {
146		other == self.as_bitslice()
147	}
148}
149
150#[cfg(not(tarpaulin_include))]
151impl<O1, O2, T1, T2> PartialOrd<BitBox<T2, O2>> for BitSlice<T1, O1>
152where
153	O1: BitOrder,
154	O2: BitOrder,
155	T1: BitStore,
156	T2: BitStore,
157{
158	#[inline]
159	fn partial_cmp(&self, other: &BitBox<T2, O2>) -> Option<cmp::Ordering> {
160		self.partial_cmp(other.as_bitslice())
161	}
162}
163
164#[cfg(not(tarpaulin_include))]
165impl<T, O, Rhs> PartialOrd<Rhs> for BitBox<T, O>
166where
167	T: BitStore,
168	O: BitOrder,
169	Rhs: ?Sized + PartialOrd<BitSlice<T, O>>,
170{
171	#[inline]
172	fn partial_cmp(&self, other: &Rhs) -> Option<cmp::Ordering> {
173		other.partial_cmp(self.as_bitslice())
174	}
175}
176
177#[cfg(not(tarpaulin_include))]
178impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<T2, O2>> for &'a BitSlice<T1, O1>
179where
180	O1: BitOrder,
181	O2: BitOrder,
182	T1: BitStore,
183	T2: BitStore,
184{
185	#[inline]
186	fn partial_cmp(&self, other: &BitBox<T2, O2>) -> Option<cmp::Ordering> {
187		self.partial_cmp(other.as_bitslice())
188	}
189}
190
191#[cfg(not(tarpaulin_include))]
192impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<T2, O2>> for &'a mut BitSlice<T1, O1>
193where
194	O1: BitOrder,
195	O2: BitOrder,
196	T1: BitStore,
197	T2: BitStore,
198{
199	#[inline]
200	fn partial_cmp(&self, other: &BitBox<T2, O2>) -> Option<cmp::Ordering> {
201		self.partial_cmp(other.as_bitslice())
202	}
203}
204
205#[cfg(not(tarpaulin_include))]
206impl<T, O> AsRef<BitSlice<T, O>> for BitBox<T, O>
207where
208	T: BitStore,
209	O: BitOrder,
210{
211	#[inline]
212	fn as_ref(&self) -> &BitSlice<T, O> {
213		self.as_bitslice()
214	}
215}
216
217#[cfg(not(tarpaulin_include))]
218impl<T, O> AsMut<BitSlice<T, O>> for BitBox<T, O>
219where
220	T: BitStore,
221	O: BitOrder,
222{
223	#[inline]
224	fn as_mut(&mut self) -> &mut BitSlice<T, O> {
225		self.as_mut_bitslice()
226	}
227}
228
229impl<T, O> From<&'_ BitSlice<T, O>> for BitBox<T, O>
230where
231	T: BitStore,
232	O: BitOrder,
233{
234	#[inline]
235	fn from(slice: &BitSlice<T, O>) -> Self {
236		slice.pipe(Self::from_bitslice)
237	}
238}
239
240impl<A, O> From<BitArray<A, O>> for BitBox<A::Store, O>
241where
242	A: BitViewSized,
243	O: BitOrder,
244{
245	#[inline]
246	fn from(array: BitArray<A, O>) -> Self {
247		array.as_bitslice().pipe(Self::from_bitslice)
248	}
249}
250
251impl<T, O> From<Box<T>> for BitBox<T, O>
252where
253	T: BitStore,
254	O: BitOrder,
255{
256	#[inline]
257	fn from(elem: Box<T>) -> Self {
258		unsafe {
259			Box::from_raw(Box::into_raw(elem).cast::<[T; 1]>() as *mut [T])
260		}
261		.pipe(Self::from_boxed_slice)
262	}
263}
264
265impl<'a, T, O> From<Cow<'a, BitSlice<T, O>>> for BitBox<T, O>
266where
267	T: BitStore,
268	O: BitOrder,
269{
270	#[inline]
271	fn from(cow: Cow<'a, BitSlice<T, O>>) -> Self {
272		cow.into_owned().into_boxed_bitslice()
273	}
274}
275
276impl<T, O> From<BitVec<T, O>> for BitBox<T, O>
277where
278	T: BitStore,
279	O: BitOrder,
280{
281	#[inline]
282	fn from(bv: BitVec<T, O>) -> Self {
283		bv.into_boxed_bitslice()
284	}
285}
286
287impl<T, O> From<BitBox<T, O>> for Box<[T]>
288where
289	T: BitStore,
290	O: BitOrder,
291{
292	#[inline]
293	fn from(bb: BitBox<T, O>) -> Self {
294		bb.into_boxed_slice()
295	}
296}
297
298impl<T, O> TryFrom<Box<[T]>> for BitBox<T, O>
299where
300	T: BitStore,
301	O: BitOrder,
302{
303	type Error = Box<[T]>;
304
305	#[inline]
306	fn try_from(boxed: Box<[T]>) -> Result<Self, Self::Error> {
307		Self::try_from_boxed_slice(boxed)
308	}
309}
310
311impl<T, O> Default for BitBox<T, O>
312where
313	T: BitStore,
314	O: BitOrder,
315{
316	#[inline]
317	fn default() -> Self {
318		Self::from_bitslice(BitSlice::<T, O>::empty())
319	}
320}
321
322impl<T, O> Debug for BitBox<T, O>
323where
324	T: BitStore,
325	O: BitOrder,
326{
327	#[inline]
328	fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
329		self.bitspan.render(fmt, "Box", None)?;
330		fmt.write_str(" ")?;
331		Display::fmt(self, fmt)
332	}
333}
334
335easy_fmt! {
336	impl Binary
337	impl Display
338	impl LowerHex
339	impl Octal
340	impl Pointer
341	impl UpperHex
342	for BitBox
343}
344
345#[cfg(not(tarpaulin_include))]
346impl<T, O, I> FromIterator<I> for BitBox<T, O>
347where
348	T: BitStore,
349	O: BitOrder,
350	BitVec<T, O>: FromIterator<I>,
351{
352	#[inline]
353	fn from_iter<II>(iter: II) -> Self
354	where II: IntoIterator<Item = I> {
355		BitVec::from_iter(iter).into_boxed_bitslice()
356	}
357}
358
359#[cfg(not(tarpaulin_include))]
360impl<T, O> Hash for BitBox<T, O>
361where
362	T: BitStore,
363	O: BitOrder,
364{
365	#[inline]
366	fn hash<H>(&self, state: &mut H)
367	where H: Hasher {
368		self.as_bitslice().hash(state)
369	}
370}
371
372unsafe impl<T, O> Send for BitBox<T, O>
373where
374	T: BitStore,
375	O: BitOrder,
376{
377}
378
379unsafe impl<T, O> Sync for BitBox<T, O>
380where
381	T: BitStore,
382	O: BitOrder,
383{
384}
385
386impl<T, O> Unpin for BitBox<T, O>
387where
388	T: BitStore,
389	O: BitOrder,
390{
391}