maxlen 0.2.0

Length-bounded string and slice/vector
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
use crate::{BVec, LengthExceeded, const_checks};
use std::{
	borrow::{Borrow, BorrowMut, Cow},
	collections::VecDeque,
	io::{BufRead, Read, Write},
	mem::transmute,
	ops::{
		Bound, Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive,
		RangeTo, RangeToInclusive,
	},
	rc::Rc,
	sync::Arc,
};

/// Bounded [`[T]`][slice].
///
/// Guaranteed to not be longer than `MAX` elements .
#[derive(Debug, Hash)]
#[repr(transparent)]
pub struct BSlice<T, const MAX: usize> {
	s: [T],
}

/// Creates a static `&'static BSlice<T, MAX>` with a compile-time check.
///
/// ```
/// # use maxlen::{bslice, BSlice};
/// let _: &BSlice<u8, 255> = bslice![];
/// let _: &BSlice<_, 255> = bslice![0; 20];
/// let _: &BSlice<_, 255> = bslice![0, 1, 2, 3, 4];
///
/// // let _: &BSlice<_, 1> = bslice![0; 2]; // will not compile
/// ```
#[macro_export]
macro_rules! bslice {
	() => {
		unsafe { $crate::BSlice::from_slice_unchecked(&[]) }
	};
	($elem:expr; $n:expr) => {
		$crate::BSlice::from_array(&[$elem; $n])
	};
	($($x:expr),+ $(,)?) => {
		$crate::BSlice::from_array(&[$($x),+])
	};
}

impl<T, const MAX: usize> BSlice<T, MAX> {
	/// Creates a `&BSlice<T, MAX>` from a slice without any checks.
	///
	/// # Safety
	///
	/// The caller is responsible for making sure that the slice is definitely not longer than `MAX` elements.
	pub const unsafe fn from_slice_unchecked(s: &[T]) -> &Self {
		unsafe { std::mem::transmute(s) }
	}
	/// Creates a `&mut BSlice<T, MAX>` from a mutable slice without any checks.
	///
	/// # Safety
	///
	/// The caller is responsible for making sure that the slice is definitely not longer than `MAX` elements.
	pub const unsafe fn from_slice_mut_unchecked(s: &mut [T]) -> &mut Self {
		unsafe { std::mem::transmute(s) }
	}
	/// Creates a `&BSlice<T, MAX>` from a slice, performing a runtime check.
	pub fn from_slice(s: &[T]) -> Result<&Self, LengthExceeded> {
		if s.len() > MAX {
			return Err(LengthExceeded {
				length: s.len(),
				maximum: MAX,
			});
		}

		Ok(unsafe { Self::from_slice_unchecked(s) })
	}
	/// Creates a `&mut BSlice<T, MAX>` from a mutable slice, performing a runtime check.
	pub fn from_slice_mut(s: &mut [T]) -> Result<&mut Self, LengthExceeded> {
		if s.len() > MAX {
			return Err(LengthExceeded {
				length: s.len(),
				maximum: MAX,
			});
		}

		Ok(unsafe { Self::from_slice_mut_unchecked(s) })
	}
	/// Constructs a `&BSlice<T, MAX>` from a `&[T; N]` using a compile-time check
	pub const fn from_array<const N: usize>(v: &[T; N]) -> &BSlice<T, MAX> {
		// compile time check
		_ = <const_checks::Pair<MAX, N> as const_checks::AssertGe>::VALID;

		unsafe { BSlice::from_slice_unchecked(v) }
	}
	/// Constructs a `&mut BSlice<T, MAX>` from a `&mut [T; N]` using a compile-time check
	pub const fn from_array_mut<const N: usize>(v: &mut [T; N]) -> &mut BSlice<T, MAX> {
		// compile time check
		_ = <const_checks::Pair<MAX, N> as const_checks::AssertGe>::VALID;

		unsafe { BSlice::from_slice_mut_unchecked(v) }
	}
	/// Relaxes the `MAX` bound, converting to a type with a bigger one.
	///
	/// This conversion is free and does not involve any checks. It is
	/// asserted at compile time that the new `MAX` is bigger than before.
	pub const fn relax_max<const MAX2: usize>(&self) -> &BSlice<T, MAX2> {
		// assert that MAX2 >= MAX at compile time
		_ = <const_checks::Pair<MAX2, MAX> as const_checks::AssertGe>::VALID;

		unsafe { BSlice::from_slice_unchecked(&self.s) }
	}
	/// Changes the `MAX` bound.
	///
	/// This involves a check whether the new bound is met.
	pub fn change_max<const MAX2: usize>(&self) -> Result<&BSlice<T, MAX2>, LengthExceeded> {
		BSlice::from_slice(self)
	}
	/// Relaxes the `MAX` bound, converting to a type with a bigger one.
	///
	/// This conversion is free and does not involve any checks. It is
	/// asserted at compile time that the new `MAX` is bigger than before.
	pub const fn relax_max_mut<const MAX2: usize>(&mut self) -> &mut BSlice<T, MAX2> {
		// assert that MAX2 >= MAX at compile time
		_ = <const_checks::Pair<MAX2, MAX> as const_checks::AssertGe>::VALID;

		unsafe { BSlice::from_slice_mut_unchecked(&mut self.s) }
	}
	/// Changes the `MAX` bound.
	///
	/// This involves a check whether the new bound is met.
	pub fn change_max_mut<const MAX2: usize>(
		&mut self,
	) -> Result<&mut BSlice<T, MAX2>, LengthExceeded> {
		BSlice::from_slice_mut(self)
	}
}

// Trait implementations relating BSlice and BVec
//////////////////////////////////////////////////

impl<T: Clone, const MAX: usize> ToOwned for BSlice<T, MAX> {
	type Owned = BVec<T, MAX>;

	fn to_owned(&self) -> Self::Owned {
		unsafe { BVec::from_vec_unchecked(self.to_vec()) }
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for BVec<T, MAX> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		value.to_owned()
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for BVec<T, MAX> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		value.to_owned()
	}
}
impl<T: PartialEq<U>, U, const MAX1: usize, const MAX2: usize> PartialEq<BVec<U, MAX2>>
	for BSlice<T, MAX1>
{
	fn eq(&self, other: &BVec<U, MAX2>) -> bool {
		(**self).eq(&***other)
	}
}
impl<T: PartialEq<U>, U, const MAX1: usize, const MAX2: usize> PartialEq<&BVec<U, MAX2>>
	for BSlice<T, MAX1>
{
	fn eq(&self, other: &&BVec<U, MAX2>) -> bool {
		(**self).eq(&****other)
	}
}
impl<T: PartialEq<U>, U, const MAX1: usize, const MAX2: usize> PartialEq<&mut BVec<U, MAX2>>
	for BSlice<T, MAX1>
{
	fn eq(&self, other: &&mut BVec<U, MAX2>) -> bool {
		(**self).eq(&****other)
	}
}

// Trait implementations mirroring standard slice
/////////////////////////////////////////////////

impl<'a, T, const MAX: usize> TryFrom<&'a [T]> for &'a BSlice<T, MAX> {
	type Error = LengthExceeded;

	fn try_from(value: &'a [T]) -> Result<Self, Self::Error> {
		BSlice::from_slice(value)
	}
}
impl<'a, T, const MAX: usize> TryFrom<&'a mut [T]> for &'a mut BSlice<T, MAX> {
	type Error = LengthExceeded;

	fn try_from(value: &'a mut [T]) -> Result<Self, Self::Error> {
		BSlice::from_slice_mut(value)
	}
}
impl<T, const MAX: usize> Deref for BSlice<T, MAX> {
	type Target = [T];

	fn deref(&self) -> &Self::Target {
		&self.s
	}
}
impl<T, const MAX: usize> DerefMut for BSlice<T, MAX> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.s
	}
}
impl<T, const MAX: usize> AsRef<Self> for BSlice<T, MAX> {
	fn as_ref(&self) -> &Self {
		self
	}
}
impl<T, const MAX: usize> AsMut<Self> for BSlice<T, MAX> {
	fn as_mut(&mut self) -> &mut Self {
		self
	}
}
impl<T, const MAX: usize> AsRef<[T]> for BSlice<T, MAX> {
	fn as_ref(&self) -> &[T] {
		self
	}
}
impl<T, const MAX: usize> AsMut<[T]> for BSlice<T, MAX> {
	fn as_mut(&mut self) -> &mut [T] {
		self
	}
}
impl<T, const MAX: usize> Borrow<[T]> for BSlice<T, MAX> {
	fn borrow(&self) -> &[T] {
		self
	}
}
impl<T, const MAX: usize> BorrowMut<[T]> for BSlice<T, MAX> {
	fn borrow_mut(&mut self) -> &mut [T] {
		self
	}
}
impl<const MAX: usize> BufRead for &BSlice<u8, MAX> {
	fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
		convert_mut_ref(self).fill_buf()
	}
	fn consume(&mut self, amt: usize) {
		convert_mut_ref(self).consume(amt)
	}
}
impl<T: Clone, const MAX: usize> Clone for Box<BSlice<T, MAX>> {
	fn clone(&self) -> Self {
		unsafe {
			Box::from_raw(Box::into_raw(self.to_vec().into_boxed_slice()) as *mut BSlice<T, MAX>)
		}
	}
}
impl<T, const MAX: usize> Default for &BSlice<T, MAX> {
	fn default() -> Self {
		unsafe { BSlice::from_slice_unchecked(Default::default()) }
	}
}
impl<T, const MAX: usize> Default for &mut BSlice<T, MAX> {
	fn default() -> Self {
		unsafe { BSlice::from_slice_mut_unchecked(Default::default()) }
	}
}
impl<T, const MAX: usize> Default for Box<BSlice<T, MAX>> {
	fn default() -> Self {
		unsafe { Box::from_raw(Box::into_raw(Box::<[T]>::default()) as *mut BSlice<T, MAX>) }
	}
}
impl<'a, T: Clone, const MAX: usize> From<&'a BSlice<T, MAX>> for Cow<'a, BSlice<T, MAX>> {
	fn from(value: &'a BSlice<T, MAX>) -> Self {
		Cow::Borrowed(value)
	}
}
impl<'a, T: Clone, const MAX: usize> From<&'a BSlice<T, MAX>> for Cow<'a, [T]> {
	fn from(value: &'a BSlice<T, MAX>) -> Self {
		Cow::Borrowed(value)
	}
}
impl<'a, T: Clone, const MAX: usize> From<&'a mut BSlice<T, MAX>> for Cow<'a, BSlice<T, MAX>> {
	fn from(value: &'a mut BSlice<T, MAX>) -> Self {
		Cow::Borrowed(value)
	}
}
impl<'a, T: Clone, const MAX: usize> From<&'a mut BSlice<T, MAX>> for Cow<'a, [T]> {
	fn from(value: &'a mut BSlice<T, MAX>) -> Self {
		Cow::Borrowed(value)
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Arc<BSlice<T, MAX>> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		Box::<BSlice<T, MAX>>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Arc<[T]> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		Box::<[T]>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Arc<BSlice<T, MAX>> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		Box::<BSlice<T, MAX>>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Arc<[T]> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		Box::<[T]>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Box<BSlice<T, MAX>> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		let b = Box::<[T]>::from(&**value);

		unsafe { Box::from_raw(Box::into_raw(b) as *mut BSlice<T, MAX>) }
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Box<[T]> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		Box::<[T]>::from(&**value)
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Box<BSlice<T, MAX>> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		let b = Box::<[T]>::from(&**value);

		unsafe { Box::from_raw(Box::into_raw(b) as *mut BSlice<T, MAX>) }
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Box<[T]> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		Box::<[T]>::from(&**value)
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Rc<BSlice<T, MAX>> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		Box::<BSlice<T, MAX>>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Rc<[T]> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		Box::<[T]>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Rc<BSlice<T, MAX>> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		Box::<BSlice<T, MAX>>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Rc<[T]> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		Box::<[T]>::from(value).into()
	}
}
impl<T: Clone, const MAX: usize> From<&BSlice<T, MAX>> for Vec<T> {
	fn from(value: &BSlice<T, MAX>) -> Self {
		value.to_vec()
	}
}
impl<T: Clone, const MAX: usize> From<&mut BSlice<T, MAX>> for Vec<T> {
	fn from(value: &mut BSlice<T, MAX>) -> Self {
		value.to_vec()
	}
}
impl<T, const MAX: usize> Index<usize> for BSlice<T, MAX> {
	type Output = T;

	fn index(&self, index: usize) -> &Self::Output {
		(**self).index(index)
	}
}
impl<T, const MAX: usize> IndexMut<usize> for BSlice<T, MAX> {
	fn index_mut(&mut self, index: usize) -> &mut Self::Output {
		(**self).index_mut(index)
	}
}
macro_rules! impl_index {
	($index:ty) => {
		impl<T, const MAX: usize> Index<$index> for BSlice<T, MAX> {
			type Output = Self;

			fn index(&self, index: $index) -> &Self::Output {
				unsafe { Self::from_slice_unchecked((**self).index(index)) }
			}
		}
		impl<T, const MAX: usize> IndexMut<$index> for BSlice<T, MAX> {
			fn index_mut(&mut self, index: $index) -> &mut Self::Output {
				unsafe { Self::from_slice_mut_unchecked((**self).index_mut(index)) }
			}
		}
	};
}
impl_index! {(Bound<usize>, Bound<usize>)}
impl_index! {Range<usize>}
impl_index! {RangeFrom<usize>}
impl_index! {RangeFull}
impl_index! {RangeInclusive<usize>}
impl_index! {RangeTo<usize>}
impl_index! {RangeToInclusive<usize>}
impl<'a, T, const MAX: usize> IntoIterator for &'a BSlice<T, MAX> {
	type Item = &'a T;
	type IntoIter = <&'a [T] as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		(&**self).into_iter()
	}
}
impl<'a, T, const MAX: usize> IntoIterator for &'a mut BSlice<T, MAX> {
	type Item = &'a mut T;
	type IntoIter = <&'a mut [T] as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		(&mut **self).into_iter()
	}
}
impl<'a, 'b, T, const MAX: usize> IntoIterator for &'b &'a BSlice<T, MAX> {
	type Item = &'b T;
	type IntoIter = <&'b [T] as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		(&**self).into_iter()
	}
}
impl<T, const MAX: usize> IntoIterator for Box<BSlice<T, MAX>> {
	type Item = T;
	type IntoIter = <Box<[T]> as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		unsafe { Box::from_raw(Box::into_raw(self) as *mut [T]) }.into_iter()
	}
}
impl<'a, T, const MAX: usize> IntoIterator for &'a Box<BSlice<T, MAX>> {
	type Item = &'a T;
	type IntoIter = <&'a [T] as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		(&**self).into_iter()
	}
}
impl<'a, T, const MAX: usize> IntoIterator for &'a mut Box<BSlice<T, MAX>> {
	type Item = &'a mut T;
	type IntoIter = <&'a mut [T] as IntoIterator>::IntoIter;

	fn into_iter(self) -> Self::IntoIter {
		(&mut **self).into_iter()
	}
}
impl<T: PartialEq<U>, U, const MAX1: usize, const MAX2: usize> PartialEq<BSlice<U, MAX2>>
	for BSlice<T, MAX1>
{
	fn eq(&self, other: &BSlice<U, MAX2>) -> bool {
		(**self).eq(&**other)
	}
}
impl<T: PartialEq<U>, U, const MAX1: usize, const MAX2: usize> PartialEq<&BSlice<U, MAX2>>
	for BSlice<T, MAX1>
{
	fn eq(&self, other: &&BSlice<U, MAX2>) -> bool {
		(**self).eq(&***other)
	}
}
impl<T: PartialEq<U>, U, const MAX1: usize, const MAX2: usize> PartialEq<&mut BSlice<U, MAX2>>
	for BSlice<T, MAX1>
{
	fn eq(&self, other: &&mut BSlice<U, MAX2>) -> bool {
		(**self).eq(&***other)
	}
}
impl<T: PartialEq<U>, U, const MAX: usize> PartialEq<BSlice<U, MAX>> for [T] {
	fn eq(&self, other: &BSlice<U, MAX>) -> bool {
		self.eq(&**other)
	}
}
impl<T: PartialEq<U>, U, const MAX: usize> PartialEq<&BSlice<U, MAX>> for [T] {
	fn eq(&self, other: &&BSlice<U, MAX>) -> bool {
		self.eq(&**other)
	}
}
impl<T: PartialEq<U>, U, const MAX: usize> PartialEq<&mut BSlice<U, MAX>> for [T] {
	fn eq(&self, other: &&mut BSlice<U, MAX>) -> bool {
		self.eq(&**other)
	}
}
impl<T: PartialEq<U>, U, const MAX: usize> PartialEq<[U]> for BSlice<T, MAX> {
	fn eq(&self, other: &[U]) -> bool {
		(**self).eq(other)
	}
}
impl<T: PartialEq<U>, U, const MAX: usize> PartialEq<[U]> for &BSlice<T, MAX> {
	fn eq(&self, other: &[U]) -> bool {
		(**self).eq(other)
	}
}
impl<T: PartialEq<U>, U, const MAX: usize> PartialEq<[U]> for &mut BSlice<T, MAX> {
	fn eq(&self, other: &[U]) -> bool {
		(**self).eq(other)
	}
}
impl<T, U, const MAX: usize, const N: usize> PartialEq<BSlice<T, MAX>> for [U; N]
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &BSlice<T, MAX>) -> bool {
		self.eq(&**other)
	}
}
impl<T, U, const MAX: usize, const N: usize> PartialEq<&BSlice<T, MAX>> for [U; N]
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &&BSlice<T, MAX>) -> bool {
		self.eq(&**other)
	}
}
impl<T, U, const MAX: usize, const N: usize> PartialEq<&mut BSlice<T, MAX>> for [U; N]
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &&mut BSlice<T, MAX>) -> bool {
		self.eq(&**other)
	}
}
impl<T, U, const MAX: usize, const N: usize> PartialEq<[U; N]> for BSlice<T, MAX>
where
	T: PartialEq<U>,
{
	fn eq(&self, other: &[U; N]) -> bool {
		(**self).eq(other)
	}
}
impl<T, U, const MAX: usize, const N: usize> PartialEq<[U; N]> for &BSlice<T, MAX>
where
	T: PartialEq<U>,
{
	fn eq(&self, other: &[U; N]) -> bool {
		(**self).eq(other)
	}
}
impl<T, U, const MAX: usize, const N: usize> PartialEq<[U; N]> for &mut BSlice<T, MAX>
where
	T: PartialEq<U>,
{
	fn eq(&self, other: &[U; N]) -> bool {
		(**self).eq(other)
	}
}
impl<T, U, const MAX: usize> PartialEq<BSlice<T, MAX>> for Cow<'_, [U]>
where
	U: PartialEq<T> + Clone,
{
	fn eq(&self, other: &BSlice<T, MAX>) -> bool {
		self.eq(&&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<&BSlice<T, MAX>> for Cow<'_, [U]>
where
	U: PartialEq<T> + Clone,
{
	fn eq(&self, other: &&BSlice<T, MAX>) -> bool {
		self.eq(&&***other)
	}
}
impl<T, U, const MAX: usize> PartialEq<&mut BSlice<T, MAX>> for Cow<'_, [U]>
where
	U: PartialEq<T> + Clone,
{
	fn eq(&self, other: &&mut BSlice<T, MAX>) -> bool {
		self.eq(&&***other)
	}
}
impl<T, U, const MAX: usize> PartialEq<Cow<'_, [U]>> for BSlice<T, MAX>
where
	T: PartialEq<U>,
	U: Clone,
{
	fn eq(&self, other: &Cow<'_, [U]>) -> bool {
		(**self).eq(&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<Cow<'_, [U]>> for &BSlice<T, MAX>
where
	T: PartialEq<U>,
	U: Clone,
{
	fn eq(&self, other: &Cow<'_, [U]>) -> bool {
		(***self).eq(&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<Cow<'_, [U]>> for &mut BSlice<T, MAX>
where
	T: PartialEq<U>,
	U: Clone,
{
	fn eq(&self, other: &Cow<'_, [U]>) -> bool {
		(***self).eq(&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<BSlice<T, MAX>> for Vec<U>
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &BSlice<T, MAX>) -> bool {
		self.eq(&&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<&BSlice<T, MAX>> for Vec<U>
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &&BSlice<T, MAX>) -> bool {
		self.eq(&&***other)
	}
}
impl<T, U, const MAX: usize> PartialEq<&mut BSlice<T, MAX>> for Vec<U>
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &&mut BSlice<T, MAX>) -> bool {
		self.eq(&&***other)
	}
}
impl<T, U, const MAX: usize> PartialEq<Vec<U>> for BSlice<T, MAX>
where
	T: PartialEq<U>,
{
	fn eq(&self, other: &Vec<U>) -> bool {
		(**self).eq(&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<Vec<U>> for &BSlice<T, MAX>
where
	T: PartialEq<U>,
{
	fn eq(&self, other: &Vec<U>) -> bool {
		(***self).eq(&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<Vec<U>> for &mut BSlice<T, MAX>
where
	T: PartialEq<U>,
{
	fn eq(&self, other: &Vec<U>) -> bool {
		(***self).eq(&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<BSlice<T, MAX>> for VecDeque<U>
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &BSlice<T, MAX>) -> bool {
		self.eq(&&**other)
	}
}
impl<T, U, const MAX: usize> PartialEq<&BSlice<T, MAX>> for VecDeque<U>
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &&BSlice<T, MAX>) -> bool {
		self.eq(&&***other)
	}
}
impl<T, U, const MAX: usize> PartialEq<&mut BSlice<T, MAX>> for VecDeque<U>
where
	U: PartialEq<T>,
{
	fn eq(&self, other: &&mut BSlice<T, MAX>) -> bool {
		self.eq(&&***other)
	}
}
impl<T: Eq, const MAX: usize> Eq for BSlice<T, MAX> {}
impl<T: PartialOrd, const MAX1: usize, const MAX2: usize> PartialOrd<BSlice<T, MAX2>>
	for BSlice<T, MAX1>
{
	fn partial_cmp(&self, other: &BSlice<T, MAX2>) -> Option<std::cmp::Ordering> {
		(**self).partial_cmp(&**other)
	}
}
impl<T: PartialOrd, const MAX: usize> PartialOrd<BSlice<T, MAX>> for [T] {
	fn partial_cmp(&self, other: &BSlice<T, MAX>) -> Option<std::cmp::Ordering> {
		self.partial_cmp(&**other)
	}
}
impl<T: PartialOrd, const MAX: usize> PartialOrd<&BSlice<T, MAX>> for [T] {
	fn partial_cmp(&self, other: &&BSlice<T, MAX>) -> Option<std::cmp::Ordering> {
		self.partial_cmp(&***other)
	}
}
impl<T: PartialOrd, const MAX: usize> PartialOrd<&mut BSlice<T, MAX>> for [T] {
	fn partial_cmp(&self, other: &&mut BSlice<T, MAX>) -> Option<std::cmp::Ordering> {
		self.partial_cmp(&***other)
	}
}
impl<T: Ord, const MAX: usize> Ord for BSlice<T, MAX> {
	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
		(**self).cmp(&**other)
	}
}
impl<const MAX: usize> Read for &BSlice<u8, MAX> {
	fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
		convert_mut_ref(self).read(buf)
	}
}
impl<const MAX: usize> Write for &mut BSlice<u8, MAX> {
	fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
		convert_mut_mut(self).write(buf)
	}
	fn flush(&mut self) -> std::io::Result<()> {
		convert_mut_mut(self).flush()
	}
}

fn convert_mut_ref<'a, 'b, T, const MAX: usize>(v: &'a mut &'b BSlice<T, MAX>) -> &'a mut &'b [T] {
	unsafe { transmute(v) }
}
fn convert_mut_mut<'a, 'b, T, const MAX: usize>(
	v: &'a mut &'b mut BSlice<T, MAX>,
) -> &'a mut &'b mut [T] {
	unsafe { transmute(v) }
}

#[cfg(feature = "serde")]
mod serde_impls {
	use super::*;
	use serde::{Deserialize, Serialize, de::Visitor, ser::SerializeSeq};

	impl<'a, T: Serialize, const MAX: usize> Serialize for &'a BSlice<T, MAX> {
		fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
		where
			S: serde::Serializer,
		{
			let mut seq = serializer.serialize_seq(Some(self.len()))?;
			for e in self {
				seq.serialize_element(e)?;
			}
			seq.end()
		}
	}

	// Deserialize only implemented for [u8] because its a SLICE.
	struct BSLiceVisitor<const MAX: usize>;
	impl<'de, const MAX: usize> Visitor<'de> for BSLiceVisitor<MAX> {
		type Value = &'de BSlice<u8, MAX>;

		fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
			formatter.write_str("a byte slice")
		}
		fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
		where
			E: serde::de::Error,
		{
			match BSlice::from_slice(v) {
				Ok(b) => Ok(b),
				Err(_e) => Err(serde::de::Error::invalid_length(
					v.len(),
					&format!("{MAX}").as_str(),
				)),
			}
		}
	}
	impl<'de, const MAX: usize> Deserialize<'de> for &'de BSlice<u8, MAX> {
		fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
		where
			D: serde::Deserializer<'de>,
		{
			deserializer.deserialize_seq(BSLiceVisitor)
		}
	}
}

#[cfg(test)]
mod tests {
	use crate::*;

	#[test]
	fn test_bslice_macro() {
		let _: &BSlice<u8, 255> = bslice![];
		let _: &BSlice<_, 255> = bslice![0; 20];
		// let _: &BSlice<_, 1> = bslice![0; 2]; // should fail
		let _: &BSlice<_, 255> = bslice![0, 1, 2, 3, 4];
		// let _: &BSlice<_, 3> = bslice![0, 1, 2, 3, 4]; // should fail
	}
}