dyn-slice 3.2.2

&dyn [Trait] implementation, inspired by a Reddit thread.
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
use core::{
    any::Any,
    borrow::{Borrow, BorrowMut},
    cmp::{Ordering, PartialEq, PartialOrd},
    convert::{AsMut, AsRef},
    fmt::{
        self, Binary, Debug, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex, Write,
    },
    future::Future,
    hash::{self, BuildHasher, Hasher},
    iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator},
    marker::{Send, Sized, Sync},
    ops::{
        AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, Deref, DerefMut, DivAssign, Index,
        IndexMut, MulAssign, RemAssign, ShlAssign, ShrAssign, SubAssign,
    },
    ptr::{DynMetadata, Pointee},
};

use crate::DynSliceMut;

use super::{declare_new_fns, DynSlice};

#[allow(unused)]
macro_rules! feature_availability {
    ( $feature:literal ) => {
        concat!(
            "(only available with the [`",
            $feature,
            "` feature](https://docs.rs/crate/dyn-slice/",
            env!("CARGO_PKG_VERSION"),
            "/features))"
        )
    };
}

declare_new_fns!(
    #[crate = crate]
    ///
    /// `DynSlice(Mut)<dyn Any>`, `DynSlice(Mut)<dyn Any + Send>` and `DynSlice(Mut)<dyn Any + Send + Sync>` have a few extra methods:
    /// - [`DynSlice::is`]
    /// - [`DynSlice::downcast`]
    /// - [`DynSliceMut::downcast_mut`]
    ///
    /// # Examples
    ///
    /// ```
    /// # use dyn_slice::standard::any;
    /// let array: [u8; 4] = [1, 2, 4, 8];
    /// let slice = any::new(&array);
    ///
    /// // Assert that the dyn-slice is a slice of `u8`s
    /// assert!(slice.is::<u8>());
    /// // Downcast the dyn-slice to a slice of `u8`s
    /// assert_eq!(slice.downcast::<u8>(), Some(array.as_slice()));
    /// ```
    ///
    /// ```
    /// # use dyn_slice::standard::any;
    /// let mut array: [u8; 4] = [1, 2, 4, 8];
    /// let mut slice = any::new_mut(&mut array);
    ///
    /// // Downcast the mutable dyn-slice to a mutable slice of `u8`s
    /// slice.downcast_mut::<u8>().unwrap()[1] = 255;
    /// assert_eq!(array, [1, 255, 4, 8]);
    /// ```
    pub any Any
);
declare_new_fns!(
    #[crate = crate]
    ///
    /// `DynSlice(Mut)<dyn Any>`, `DynSlice(Mut)<dyn Any + Send>` and `DynSlice(Mut)<dyn Any + Send + Sync>` have a few extra methods:
    /// - [`DynSlice::is`]
    /// - [`DynSlice::downcast`]
    /// - [`DynSliceMut::downcast_mut`]
    ///
    /// # Examples
    ///
    /// ```
    /// # use dyn_slice::standard::any_send;
    /// let array: [u8; 4] = [1, 2, 4, 8];
    /// let slice = any_send::new(&array);
    ///
    /// // Assert that the dyn-slice is a slice of `u8`s
    /// assert!(slice.is::<u8>());
    /// // Downcast the dyn-slice to a slice of `u8`s
    /// assert_eq!(slice.downcast::<u8>(), Some(array.as_slice()));
    /// ```
    ///
    /// ```
    /// # use dyn_slice::standard::any_send;
    /// let mut array: [u8; 4] = [1, 2, 4, 8];
    /// let mut slice = any_send::new_mut(&mut array);
    ///
    /// // Downcast the mutable dyn-slice to a mutable slice of `u8`s
    /// slice.downcast_mut::<u8>().unwrap()[1] = 255;
    /// assert_eq!(array, [1, 255, 4, 8]);
    /// ```
    pub any_send Any + Send
);
declare_new_fns!(
    #[crate = crate]
    ///
    /// `DynSlice(Mut)<dyn Any>`, `DynSlice(Mut)<dyn Any + Send>` and `DynSlice(Mut)<dyn Any + Send + Sync>` have a few extra methods:
    /// - [`DynSlice::is`]
    /// - [`DynSlice::downcast`]
    /// - [`DynSliceMut::downcast_mut`]
    ///
    /// # Examples
    ///
    /// ```
    /// # use dyn_slice::standard::any_sync_send;
    /// let array: [u8; 4] = [1, 2, 4, 8];
    /// let slice = any_sync_send::new(&array);
    ///
    /// // Assert that the dyn-slice is a slice of `u8`s
    /// assert!(slice.is::<u8>());
    /// // Downcast the dyn-slice to a slice of `u8`s
    /// assert_eq!(slice.downcast::<u8>(), Some(array.as_slice()));
    /// ```
    ///
    /// ```
    /// # use dyn_slice::standard::any_sync_send;
    /// let mut array: [u8; 4] = [1, 2, 4, 8];
    /// let mut slice = any_sync_send::new_mut(&mut array);
    ///
    /// // Downcast the mutable dyn-slice to a mutable slice of `u8`s
    /// slice.downcast_mut::<u8>().unwrap()[1] = 255;
    /// assert_eq!(array, [1, 255, 4, 8]);
    /// ```
    pub any_sync_send Any + Sync + Send
);
macro_rules! impl_any_methods {
    ( $( $t:ty ),* ) => {
        $(
            impl<'a> DynSlice<'a, $t> {
                /// Returns `true` if the underlying slice is of type `T`.
                #[must_use]
                pub fn is<T: 'static>(&self) -> bool {
                    self.get(0).map_or(true, <$t>::is::<T>)
                }

                /// Returns the underlying slice as `&[T]`, or `None` if the underlying slice is not of type `T`.
                #[must_use]
                pub fn downcast<T: 'static>(&self) -> Option<&[T]> {
                    self.is::<T>().then(|| {
                        // SAFETY:
                        // The above line guarantees that the underlying slice is of type `T`,
                        // so the downcast is valid.
                        unsafe { self.downcast_unchecked() }
                    })
                }
            }

            impl<'a> DynSliceMut<'a, $t> {
                /// Returns the underlying slice as `&mut [T]`, or `None` if the underlying slice is not of type `T`.
                #[must_use]
                pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut [T]> {
                    self.0.is::<T>().then(|| {
                        // SAFETY:
                        // The above line guarantees that the underlying slice is of type `T`,
                        // so the downcast is valid.
                        unsafe { self.downcast_unchecked_mut() }
                    })
                }
            }
        )*
    };
}
impl_any_methods!(dyn Any, dyn Any + Send, dyn Any + Sync + Send);

declare_new_fns!(
    #[crate = crate]
    pub borrow<Borrowed> Borrow<Borrowed>
);
declare_new_fns!(
    #[crate = crate]
    pub borrow_mut<Borrowed> BorrowMut<Borrowed>
);

declare_new_fns!(
    #[crate = crate]
    ///
    /// `DynSlice(Mut)<dyn PartialEq<Rhs>>` implements `PartialEq<[Rhs]>`
    ///
    /// # Example
    /// ```
    /// # use dyn_slice::standard::partial_eq;
    /// let array: [u8; 4] = [1, 2, 4, 8];
    /// let slice = partial_eq::new(&array);
    ///
    /// assert!(slice == array.as_slice());
    /// ```
    pub partial_eq<Rhs> PartialEq<Rhs>
);
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]>
    for DynSlice<'a, Dyn>
{
    fn eq(&self, other: &[Rhs]) -> bool {
        if self.len() != other.len() {
            return false;
        }

        self.iter().zip(other.iter()).all(|(a, b)| a == b)
    }
}
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]>
    for DynSliceMut<'a, Dyn>
{
    #[inline]
    fn eq(&self, other: &[Rhs]) -> bool {
        self.0.eq(other)
    }
}
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]>
    for DynSlice<'a, Dyn>
{
    #[inline]
    fn eq(&self, other: &&[Rhs]) -> bool {
        self.eq(*other)
    }
}
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]>
    for DynSliceMut<'a, Dyn>
{
    #[inline]
    fn eq(&self, other: &&[Rhs]) -> bool {
        self.0.eq(*other)
    }
}
declare_new_fns!(
    #[crate = crate]
    ///
    /// `DynSlice(Mut)<dyn PartialOrd<Rhs>>` implements `PartialOrd<[Rhs]>`.
    /// Slices are compared [lexicographically](https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#lexicographical-comparison).
    ///
    /// # Example
    /// ```
    /// # use dyn_slice::standard::partial_ord;
    /// let array: [u8; 4] = [1, 2, 4, 8];
    /// let slice = partial_ord::new(&array);
    ///
    /// assert!(slice > &[1, 2, 3, 4][..]);
    /// assert!(slice == &array[..]);
    /// assert!(slice < &[1, 2, 4, 8, 16][..]);
    /// ```
    pub partial_ord<Rhs> PartialOrd<Rhs>
);
/// Implements comparison of slices [lexicographically](https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#lexicographical-comparison).
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs>
    PartialOrd<[Rhs]> for DynSlice<'a, Dyn>
{
    fn partial_cmp(&self, other: &[Rhs]) -> Option<Ordering> {
        let mut i1 = self.iter();
        let mut i2 = other.iter();

        loop {
            return Some(match (i1.next(), i2.next()) {
                (Some(a), Some(b)) => match a.partial_cmp(b)? {
                    Ordering::Equal => continue,
                    order => order,
                },
                (Some(_), None) => Ordering::Greater,
                (None, Some(_)) => Ordering::Less,
                (None, None) => Ordering::Equal,
            });
        }
    }
}
/// Implements comparison of slices [lexicographically](https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#lexicographical-comparison).
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs>
    PartialOrd<[Rhs]> for DynSliceMut<'a, Dyn>
{
    #[inline]
    fn partial_cmp(&self, other: &[Rhs]) -> Option<Ordering> {
        self.0.partial_cmp(other)
    }
}
/// Implements comparison of slices [lexicographically](https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#lexicographical-comparison).
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs>
    PartialOrd<&[Rhs]> for DynSlice<'a, Dyn>
{
    #[inline]
    fn partial_cmp(&self, other: &&[Rhs]) -> Option<Ordering> {
        self.partial_cmp(*other)
    }
}
/// Implements comparison of slices [lexicographically](https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#lexicographical-comparison).
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs>
    PartialOrd<&[Rhs]> for DynSliceMut<'a, Dyn>
{
    #[inline]
    fn partial_cmp(&self, other: &&[Rhs]) -> Option<Ordering> {
        self.0.partial_cmp(*other)
    }
}

declare_new_fns!(
    #[crate = crate]
    pub as_ref<T> AsRef<T>
);
declare_new_fns!(
    #[crate = crate]
    pub as_mut<T> AsMut<T>
);

declare_new_fns!(
    #[crate = crate]
    pub binary Binary
);
declare_new_fns!(
    #[crate = crate]
    ///
    /// # Example
    /// ```
    /// # use dyn_slice::standard::debug;
    /// let array: [u8; 4] = [1, 2, 4, 8];
    /// let slice = debug::new(&array);
    ///
    /// assert_eq!(
    ///     format!("{slice:?}"),
    ///     "[1, 2, 4, 8]",
    /// );
    /// ```
    pub debug Debug
);
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug for DynSlice<'a, Dyn> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug
    for DynSliceMut<'a, Dyn>
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <DynSlice<Dyn> as Debug>::fmt(&self.0, f)
    }
}
declare_new_fns!(
    #[crate = crate]
    pub display Display
);
declare_new_fns!(
    #[crate = crate]
    pub lower_exp LowerExp
);
declare_new_fns!(
    #[crate = crate]
    pub lower_hex LowerHex
);
declare_new_fns!(
    #[crate = crate]
    pub octal Octal
);
declare_new_fns!(
    #[crate = crate]
    pub pointer Pointer
);
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + ?Sized> Pointer for DynSlice<'a, Dyn> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <*const () as Pointer>::fmt(&self.data, f)
    }
}
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + ?Sized> Pointer for DynSliceMut<'a, Dyn> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <*const () as Pointer>::fmt(&self.data, f)
    }
}
declare_new_fns!(
    #[crate = crate]
    pub upper_exp UpperExp
);
declare_new_fns!(
    #[crate = crate]
    pub upper_hex UpperHex
);
declare_new_fns!(
    #[crate = crate]
    pub write Write
);

declare_new_fns!(
    #[crate = crate]
    pub future<Output> Future<Output = Output>
);

declare_new_fns!(
    #[crate = crate]
    pub build_hasher<Hasher: hash::Hasher> BuildHasher<Hasher = Hasher>
);
declare_new_fns!(
    #[crate = crate]
    pub hasher Hasher
);

declare_new_fns!(
    #[crate = crate]
    ///
    /// # Example
    /// ```
    /// # use dyn_slice::standard::double_ended_iterator;
    /// let mut array = [[1, 2].into_iter(), [3, 4].into_iter(), [5, 6].into_iter()];
    /// let mut slice = double_ended_iterator::new_mut(&mut array);
    /// let mut iter = slice.iter_mut().flatten();
    ///
    /// assert_eq!(iter.next_back().unwrap(), 6);
    /// assert_eq!(iter.next_back().unwrap(), 5);
    /// assert_eq!(iter.next_back().unwrap(), 4);
    /// assert_eq!(iter.next_back().unwrap(), 3);
    /// assert_eq!(iter.next_back().unwrap(), 2);
    /// assert_eq!(iter.next_back().unwrap(), 1);
    /// ```
    pub double_ended_iterator<Item> DoubleEndedIterator<Item = Item>
);
declare_new_fns!(
    #[crate = crate]
    pub exact_size_iterator<Item> ExactSizeIterator<Item = Item>
);
declare_new_fns!(
    #[crate = crate]
    pub fused_iterator<Item> FusedIterator<Item = Item>
);
declare_new_fns!(
    #[crate = crate]
    ///
    /// # Examples
    ///
    /// ```
    /// # use dyn_slice::standard::iterator;
    /// let mut array = [(1..5), (2..9), (4..6)];
    /// let mut slice = iterator::new_mut(&mut array);
    ///
    /// assert_eq!(slice[0].next(), Some(1));
    /// assert_eq!(slice[1].next(), Some(2));
    /// assert_eq!(slice[2].next(), Some(4));
    ///
    /// assert_eq!(slice[0].next(), Some(2));
    /// ```
    ///
    /// ```
    /// # use dyn_slice::standard::iterator;
    /// let mut array = [[1, 2].into_iter(), [3, 4].into_iter(), [5, 6].into_iter()];
    /// let mut slice = iterator::new_mut(&mut array);
    /// let mut iter = slice.iter_mut().flatten();
    ///
    /// assert_eq!(iter.next().unwrap(), 1);
    /// assert_eq!(iter.next().unwrap(), 2);
    /// assert_eq!(iter.next().unwrap(), 3);
    /// assert_eq!(iter.next().unwrap(), 4);
    /// assert_eq!(iter.next().unwrap(), 5);
    /// assert_eq!(iter.next().unwrap(), 6);
    /// ```
    pub iterator<Item> Iterator<Item = Item>
);

declare_new_fns!(
    #[crate = crate]
    pub add_assign<Rhs> AddAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub bit_and_assign<Rhs> BitAndAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub bit_or_assign<Rhs> BitOrAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub bit_xor_assign<Rhs> BitXorAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub deref<Target> Deref<Target = Target>
);
declare_new_fns!(
    #[crate = crate]
    pub deref_mut<Target> DerefMut<Target = Target>
);
declare_new_fns!(
    #[crate = crate]
    pub div_assign<Rhs> DivAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub index<Idx: ?Sized, Output: ?Sized> Index<Idx, Output = Output>
);
declare_new_fns!(
    #[crate = crate]
    pub index_mut<Idx: ?Sized, Output: ?Sized> IndexMut<Idx, Output = Output>
);
declare_new_fns!(
    #[crate = crate]
    pub mul_assign<Rhs> MulAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub rem_assign<Rhs> RemAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub shl_assign<Rhs> ShlAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub shr_assign<Rhs> ShrAssign<Rhs>
);
declare_new_fns!(
    #[crate = crate]
    pub sub_assign<Rhs> SubAssign<Rhs>
);

/// A reference-to-value conversion.
pub trait To<T> {
    /// Converts this reference into the (usually inferred) input type.
    fn to(&self) -> T;
}

// From implies Into, so Into is used to include both traits
impl<T, F: Into<T> + Copy> To<T> for F {
    #[inline]
    fn to(&self) -> T {
        (*self).into()
    }
}

declare_new_fns!(
    #[crate = crate]
    pub to<T> To<T>
);

#[cfg(feature = "alloc")]
mod standard_alloc {
    extern crate alloc;
    use alloc::string::ToString;

    use crate::declare_new_fns;

    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "alloc")))]
        #[doc = feature_availability!("alloc")]
        pub to_string ToString
    );
}
#[cfg(feature = "alloc")]
pub use standard_alloc::*;

#[cfg(feature = "std")]
mod standard_std {
    use std::{
        error::Error,
        io::{BufRead, IsTerminal, Read, Seek, Write},
        net::ToSocketAddrs,
    };

    use crate::declare_new_fns;

    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub error Error
    );

    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub buf_read BufRead
    );
    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub is_terminal IsTerminal
    );
    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub io_read Read
    );
    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub seek Seek
    );
    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub io_write Write
    );

    declare_new_fns!(
        #[crate = crate]
        #[cfg_attr(doc, doc(cfg(feature = "std")))]
        #[doc = feature_availability!("std")]
        pub to_socket_addrs<Iter: core::iter::Iterator<Item = std::net::SocketAddr>>
            ToSocketAddrs<Iter = Iter>
    );
}
#[cfg(feature = "std")]
pub use standard_std::*;

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

    #[test]
    fn test_any() {
        #[derive(Debug, PartialEq)]
        struct A;

        let array = [A, A];
        let slice = any::new(&array);

        assert!(slice.is::<A>());
        assert!(!slice.is::<u8>());

        assert_eq!(slice.downcast::<A>(), Some(&array[..]));
        assert_eq!(slice.downcast::<u8>(), None);

        // Using for loop rather than iter to make sure exactly 2 elements are
        // checked, without trusting iter
        for i in 0..array.len() {
            assert!(slice.get(i).expect("expected an element").is::<A>());
        }

        // Make sure the slice can be downcast to anything when empty

        let array: [A; 0] = [];
        let slice = any::new(&array);

        assert!(slice.is::<A>());
        assert!(slice.is::<u8>());

        assert_eq!(slice.downcast::<A>(), Some(&array[..]));
        assert_eq!(slice.downcast::<u8>(), Some(&[][..]));
    }

    #[test]
    fn test_borrow() {
        let a: Box<u8> = Box::new(5);
        let b: Box<u8> = Box::new(7);

        let array = [a, b];
        let slice = borrow::new::<u8, _>(&array);

        for (i, y) in array.iter().enumerate() {
            assert_eq!(slice.get(i).expect("expected an element").borrow(), &**y);
        }
    }

    #[test]
    fn test_partial_eq() {
        let array: [u8; 2] = [5, 7];
        let slice = partial_eq::new::<u8, _>(&array);

        for (i, y) in array.iter().enumerate() {
            let element = slice.get(i).expect("expected an element");
            assert!(element == y);
            assert!(element != &200);
        }
    }

    #[test]
    fn test_partial_eq_impl() {
        let s: &[u8] = &[10, 11, 12];
        let slice = partial_eq::new::<u8, _>(s);

        let ne1: &[u8] = &[10, 11, 22];
        let ne2: &[u8] = &[10, 21, 12];
        let ne3: &[u8] = &[20, 11, 12];
        let ne4: &[u8] = &[10, 11, 12, 13];
        let ne5: &[u8] = &[10, 11];
        let ne6: &[u8] = &[10];
        let ne7: &[u8] = &[];

        assert!(slice == s);

        assert!(slice != ne1);
        assert!(slice != ne2);
        assert!(slice != ne3);
        assert!(slice != ne4);
        assert!(slice != ne5);
        assert!(slice != ne6);
        assert!(slice != ne7);
    }

    #[test]
    fn test_partial_ord() {
        let array: [u8; 2] = [5, 7];
        let slice = partial_ord::new::<u8, _>(&array);

        for (i, y) in array.iter().enumerate() {
            let element = slice.get(i).expect("expected an element");
            assert!(element > &3);
            assert!(element == y);
            assert!(element < &10);
        }
    }

    #[test]
    fn test_partial_ord_impl() {
        let s: &[u8] = &[10, 11, 12];
        let slice = partial_ord::new::<u8, _>(s);

        let l1: &[u8] = &[10, 11, 2];
        let l2: &[u8] = &[10, 1, 12];
        let l3: &[u8] = &[0, 11, 12];
        let l4: &[u8] = &[10, 11, 2, 3];
        let l5: &[u8] = &[10, 1, 12, 3];
        let l6: &[u8] = &[0, 11, 12, 3];
        let l7: &[u8] = &[10, 11];
        let l8: &[u8] = &[10];
        let l9: &[u8] = &[];

        let g1: &[u8] = &[10, 11, 22];
        let g2: &[u8] = &[10, 21, 12];
        let g3: &[u8] = &[20, 11, 12];
        let g4: &[u8] = &[10, 21];
        let g5: &[u8] = &[20];
        let g6: &[u8] = &[10, 11, 12, 3];

        assert_eq!(slice.partial_cmp(s), Some(Ordering::Equal));

        assert!(s > l1);
        assert!(s > l2);
        assert!(s > l3);
        assert!(s > l4);
        assert!(s > l5);
        assert!(s > l6);
        assert!(s > l7);
        assert!(s > l8);
        assert!(s > l9);

        assert!(s < g1);
        assert!(s < g2);
        assert!(s < g3);
        assert!(s < g4);
        assert!(s < g5);
        assert!(s < g6);
    }

    #[test]
    fn test_as_ref() {
        let a: Box<u8> = Box::new(5);
        let b: Box<u8> = Box::new(7);

        let array = [a, b];
        let slice = as_ref::new::<u8, _>(&array);

        for (i, y) in array.iter().enumerate() {
            assert_eq!(slice.get(i).expect("expected an element").as_ref(), &**y);
        }
    }

    #[test]
    fn test_debug() {
        #[derive(Debug)]
        struct A;
        let debugged = format!("{A:?}");

        let array = [A, A];
        let slice = debug::new(&array);

        for i in 0..array.len() {
            let element = slice.get(i).expect("expected an element");
            assert_eq!(format!("{element:?}"), debugged);
        }

        assert_eq!(format!("{slice:?}"), format!("{array:?}"));

        let slice = debug::new::<A>(&[]);
        assert_eq!(format!("{slice:?}"), "[]");

        let array = [A];
        let slice = debug::new(&array);
        assert_eq!(format!("{slice:?}"), format!("{array:?}"));
    }

    #[test]
    fn test_display() {
        struct A;
        impl fmt::Display for A {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "A displayed")
            }
        }
        let displayed = format!("{A}");

        let array = [A, A];
        let slice = display::new(&array);

        for i in 0..array.len() {
            let element = slice.get(i).expect("expected an element");
            assert_eq!(format!("{element}"), displayed);
        }
    }

    #[test]
    fn test_hasher() {
        use std::collections::hash_map::DefaultHasher;

        const TEST_DATA: &[u8] = b"test hash data";

        let mut reference = DefaultHasher::new();
        reference.write(TEST_DATA);
        let reference = reference.finish();

        let mut array = [DefaultHasher::new(), DefaultHasher::new()];
        let mut slice = hasher::new_mut(&mut array);

        for hasher in &mut slice {
            hasher.write(TEST_DATA);

            assert_eq!(hasher.finish(), reference);
        }
    }

    #[test]
    fn test_iterator() {
        let mut array = [(0..5), (10..15), (-30..-25)];
        let mut slice = iterator::new_mut(&mut array);

        for (range, expected) in slice.iter_mut().zip([0, 10, -30]) {
            assert_eq!(range.next(), Some(expected));
        }

        for (range, expected) in slice.iter_mut().zip([1, 11, -29]) {
            assert_eq!(range.next(), Some(expected));
        }
    }

    #[test]
    fn test_to() {
        use core::num::NonZeroU8;

        let a: u8 = 5;
        let b: u8 = <u8 as To<u8>>::to(&a);

        assert_eq!(a, b);

        let b: u16 = <u8 as To<u16>>::to(&a);
        let a: u16 = a.into();

        assert_eq!(a, b);

        let array: [NonZeroU8; 2] = {
            // SAFETY:
            // NonZeroU8 has the same layout as u8, and can therefore be transmuted.
            unsafe { [NonZeroU8::new_unchecked(5), NonZeroU8::new_unchecked(7)] }
        };
        let slice = to::new::<u8, _>(&array);

        for (i, y) in array.iter().enumerate() {
            let element = slice.get(i).expect("expected an element");
            assert_eq!(element.to(), y.get());
        }
    }

    #[test]
    fn test_to_string() {
        struct A;
        impl core::fmt::Display for A {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "A displayed")
            }
        }
        let displayed = A.to_string();

        let array = [A, A];
        let slice = to_string::new(&array);

        for i in 0..array.len() {
            let element = slice.get(i).expect("expected an element");
            assert_eq!(element.to_string(), displayed);
        }
    }

    #[test]
    fn test_error() {
        #[derive(Debug)]
        struct A;
        impl fmt::Display for A {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "A displayed")
            }
        }
        impl std::error::Error for A {}
        let displayed = format!("{A}");

        let array = [A, A];
        let slice = error::new(&array);

        for i in 0..array.len() {
            let element = slice.get(i).expect("expected an element");
            assert_eq!(format!("{element}"), displayed);
        }

        assert_eq!(format!("{slice:?}"), format!("{array:?}"));
    }
}