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
//! Contains the ffi-safe equivalent of `&'a mut [T]`.

use std::{
    borrow::{Borrow, BorrowMut},
    io::{self, Write},
    marker::PhantomData,
    mem,
    ops::{Deref, DerefMut, Index, IndexMut},
    slice::SliceIndex,
};

use serde::{Serialize, Serializer};

#[allow(unused_imports)]
use core_extensions::SelfOps;

use crate::std_types::{RSlice, RVec};

mod privacy {
    use super::*;

    /// Ffi-safe equivalent of `&'a mut [T]`
    ///
    /// As of the writing this documentation the abi stability of `&mut [T]` is
    /// not yet guaranteed.
    ///
    /// # Lifetime problems
    ///
    /// Because `RSliceMut` dereferences into a mutable slice, you can call slice methods on it.
    ///
    /// If you call a slice method that returns a borrow into the slice,
    /// it will have the lifetime of the `let slice: RSliceMut<'a, [T]>` variable instead of the `'a`
    /// lifetime that it's parameterized over.
    ///
    /// To get a slice with the same lifetime as an `RSliceMut`,
    /// one must use one of the `RSliceMut::{into_slice, into_mut_slice}` methods.
    ///
    ///
    /// Example of what would not work:
    ///
    /// ```compile_fail
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// fn into_slice<'a, T>(slic: RSliceMut<'a, T>) -> &'a [T] {
    ///     &*slic
    /// }
    ///
    /// fn into_mut_slice<'a, T>(slic: RSliceMut<'a, T>) -> &'a mut [T] {
    ///     &mut *slic
    /// }
    /// ```
    ///
    /// Example of what would work:
    ///
    /// ```
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// fn into_slice<'a, T>(slic: RSliceMut<'a, T>) -> &'a [T] {
    ///     slic.into_slice()
    /// }
    ///
    /// fn into_mut_slice<'a, T>(slic: RSliceMut<'a, T>) -> &'a mut [T] {
    ///     slic.into_mut_slice()
    /// }
    ///
    /// ```
    ///
    ///
    /// # Example
    ///
    /// Defining an extern fn that returns a mutable reference to
    /// the first element that compares equal to a parameter.
    ///
    /// ```
    /// use abi_stable::{sabi_extern_fn, std_types::RSliceMut};
    ///
    /// #[sabi_extern_fn]
    /// pub fn find_first_mut<'a, T>(
    ///     slice_: RSliceMut<'a, T>,
    ///     element: &T,
    /// ) -> Option<&'a mut T>
    /// where
    ///     T: std::cmp::PartialEq,
    /// {
    ///     slice_
    ///         .iter()
    ///         .position(|x| x == element)
    ///         .map(|i| &mut slice_.into_mut_slice()[i])
    /// }
    /// ```
    ///
    #[repr(C)]
    #[derive(StableAbi)]
    #[sabi(bound(T: 'a))]
    pub struct RSliceMut<'a, T> {
        data: *mut T,
        length: usize,
        _marker: PhantomData<MutWorkaround<'a, T>>,
    }

    /// Used as a workaround to make `from_raw_parts_mut` a const fn.
    #[repr(C)]
    #[derive(StableAbi)]
    #[sabi(bound(T: 'a))]
    struct MutWorkaround<'a, T>(&'a mut T);

    impl_from_rust_repr! {
        impl['a, T] From<&'a mut [T]> for RSliceMut<'a, T> {
            fn(this){
                RSliceMut {
                    data: this.as_mut_ptr(),
                    length: this.len(),
                    _marker: Default::default(),
                }
            }
        }
    }

    impl<'a, T> RSliceMut<'a, T> {
        #[inline(always)]
        pub(super) const fn data(&self) -> *mut T {
            self.data
        }

        /// Gets a raw pointer to the start of the slice.
        pub const fn as_ptr(&self) -> *const T {
            self.data
        }

        /// Gets a mutable raw pointer to the start of the slice.
        pub fn as_mut_ptr(&mut self) -> *mut T {
            self.data
        }

        /// Gets a mutable raw pointer to the start of the slice.
        pub const fn into_mut_ptr(self) -> *mut T {
            self.data
        }

        /// The length (in elements) of this slice.
        ///
        /// # Example
        ///
        /// ```
        /// use abi_stable::std_types::RSliceMut;
        ///
        /// assert_eq!(RSliceMut::<u8>::from_mut_slice(&mut []).len(), 0);
        /// assert_eq!(RSliceMut::from_mut_slice(&mut [0]).len(), 1);
        /// assert_eq!(RSliceMut::from_mut_slice(&mut [0, 1]).len(), 2);
        ///
        /// ```
        #[inline(always)]
        pub const fn len(&self) -> usize {
            self.length
        }

        /// Whether this slice is empty.
        ///
        /// # Example
        ///
        /// ```
        /// use abi_stable::std_types::RSliceMut;
        ///
        /// assert_eq!(RSliceMut::<u8>::from_mut_slice(&mut []).is_empty(), true);
        /// assert_eq!(RSliceMut::from_mut_slice(&mut [0]).is_empty(), false);
        /// assert_eq!(RSliceMut::from_mut_slice(&mut [0, 1]).is_empty(), false);
        ///
        /// ```
        #[inline]
        pub const fn is_empty(&self) -> bool {
            self.length == 0
        }

        /// Constructs an `RSliceMut<'a, T>` from a pointer to the first element,
        /// and a length.
        ///
        /// # Safety
        ///
        /// Callers must ensure that:
        ///
        /// - `ptr_` points to valid memory,
        ///
        /// - `ptr_ .. ptr+len` range is accessible memory.
        ///
        /// - `ptr_` is aligned to `T`.
        ///
        /// - the data `ptr_` points to must be valid for the lifetime of this `RSlice<'a, T>`
        ///
        /// # Examples
        ///
        /// This function unsafely converts a `&mut [T]` to an `RSliceMut<T>`,
        /// equivalent to doing `RSliceMut::from_mut_slice`.
        ///
        /// ```
        /// use abi_stable::std_types::RSliceMut;
        ///
        /// fn convert<T>(slice_: &mut [T]) -> RSliceMut<'_, T> {
        ///     let len = slice_.len();
        ///     unsafe { RSliceMut::from_raw_parts_mut(slice_.as_mut_ptr(), len) }
        /// }
        ///
        /// ```
        pub const unsafe fn from_raw_parts_mut(ptr_: *mut T, len: usize) -> Self {
            Self {
                data: ptr_,
                length: len,
                // WHAT!?
                // error[E0723]: mutable references in const fn are unstable (see issue #57563)
                _marker: PhantomData,
            }
        }
    }
}
pub use self::privacy::RSliceMut;

impl<'a, T> RSliceMut<'a, T> {
    // pub const fn empty() -> Self {
    //     Self::EMPTY
    // }

    /// Converts a mutable reference to `T` to a single element `RSliceMut<'a, T>`.
    ///
    /// Note: this function does not copy anything.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// assert_eq!(
    ///     RSliceMut::from_mut(&mut 0),
    ///     RSliceMut::from_mut_slice(&mut [0])
    /// );
    /// assert_eq!(
    ///     RSliceMut::from_mut(&mut 1),
    ///     RSliceMut::from_mut_slice(&mut [1])
    /// );
    /// assert_eq!(
    ///     RSliceMut::from_mut(&mut 2),
    ///     RSliceMut::from_mut_slice(&mut [2])
    /// );
    ///
    /// ```
    pub fn from_mut(ref_: &'a mut T) -> Self {
        unsafe { Self::from_raw_parts_mut(ref_, 1) }
    }

    /// Converts a `&'a mut [T]` to a `RSliceMut<'a, T>`.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// let empty: &mut [u8] = &mut [];
    ///
    /// assert_eq!(
    ///     RSliceMut::<u8>::from_mut_slice(&mut []).as_mut_slice(),
    ///     empty
    /// );
    /// assert_eq!(
    ///     RSliceMut::from_mut_slice(&mut [0]).as_mut_slice(),
    ///     &mut [0][..]
    /// );
    /// assert_eq!(
    ///     RSliceMut::from_mut_slice(&mut [0, 1]).as_mut_slice(),
    ///     &mut [0, 1][..]
    /// );
    ///
    /// ```
    #[inline]
    pub fn from_mut_slice(slic: &'a mut [T]) -> Self {
        slic.into()
    }

    /// Creates an `RSlice<'b, T>` with access to the `range` range of elements.
    ///
    /// This is an inherent method instead of an implementation of the
    /// `std::ops::Index` trait because it does not return a reference.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::{RSlice, RSliceMut};
    ///
    /// let slic = &mut [0, 1, 2, 3];
    /// let slic = RSliceMut::from_mut_slice(slic);
    ///
    /// assert_eq!(slic.slice(..), RSlice::from_slice(&[0, 1, 2, 3]));
    /// assert_eq!(slic.slice(..2), RSlice::from_slice(&[0, 1]));
    /// assert_eq!(slic.slice(2..), RSlice::from_slice(&[2, 3]));
    /// assert_eq!(slic.slice(1..3), RSlice::from_slice(&[1, 2]));
    ///
    /// ```
    #[allow(clippy::needless_lifetimes)]
    pub fn slice<'b, I>(&'b self, i: I) -> RSlice<'b, T>
    where
        [T]: Index<I, Output = [T]>,
    {
        self.as_slice().index(i).into()
    }

    /// Creates an `RSliceMut<'a, T>` with access to the `range` range of elements.
    ///
    /// This is an inherent method instead of an implementation of the
    /// `std::ops::IndexMut` trait because it does not return a reference.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// let slic = &mut [0, 1, 2, 3];
    /// let mut slic = RSliceMut::from_mut_slice(slic);
    ///
    /// assert_eq!(
    ///     slic.slice_mut(..),
    ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3])
    /// );
    /// assert_eq!(slic.slice_mut(..2), RSliceMut::from_mut_slice(&mut [0, 1]));
    /// assert_eq!(slic.slice_mut(2..), RSliceMut::from_mut_slice(&mut [2, 3]));
    /// assert_eq!(slic.slice_mut(1..3), RSliceMut::from_mut_slice(&mut [1, 2]));
    ///
    /// ```
    #[allow(clippy::needless_lifetimes)]
    pub fn slice_mut<'b, I>(&'b mut self, i: I) -> RSliceMut<'b, T>
    where
        [T]: IndexMut<I, Output = [T]>,
    {
        self.as_mut_slice().index_mut(i).into()
    }

    /// Creates a new `RVec<T>` and clones all the elements of this slice into it.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::{RSliceMut, RVec};
    ///
    /// let slic = &mut [0, 1, 2, 3];
    /// let slic = RSliceMut::from_mut_slice(slic);
    ///
    /// assert_eq!(slic.slice(..).to_rvec(), RVec::from_slice(&[0, 1, 2, 3]));
    /// assert_eq!(slic.slice(..2).to_rvec(), RVec::from_slice(&[0, 1]));
    /// assert_eq!(slic.slice(2..).to_rvec(), RVec::from_slice(&[2, 3]));
    /// assert_eq!(slic.slice(1..3).to_rvec(), RVec::from_slice(&[1, 2]));
    ///
    /// ```
    pub fn to_rvec(&self) -> RVec<T>
    where
        T: Clone,
    {
        self.to_vec().into()
    }

    conditionally_const! {
        feature = "rust_1_64"
        ;
        unsafe fn as_slice_unbounded_lifetime(&self) -> &'a [T] {
            unsafe { ::std::slice::from_raw_parts(self.data(), self.len()) }
        }
    }

    unsafe fn as_mut_slice_unbounded_lifetime(&mut self) -> &'a mut [T] {
        unsafe { ::std::slice::from_raw_parts_mut(self.data(), self.len()) }
    }

    conditionally_const! {
        feature = "rust_1_64"
        /// Creates an `&'_ [T]` with access to all the elements of this slice.
        ///
        ;
        ///
        /// # Example
        ///
        /// ```
        /// use abi_stable::std_types::RSliceMut;
        ///
        /// assert_eq!(
        ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3]).as_slice(),
        ///     &[0, 1, 2, 3]
        /// );
        ///
        /// ```
        pub fn as_slice(&self) -> &[T] {
            unsafe { self.as_slice_unbounded_lifetime() }
        }
    }

    conditionally_const! {
        feature = "rust_1_64"
        /// Creates an `&'a [T]` with access to all the elements of this slice.
        ///
        /// This is different to `as_slice` in that the returned lifetime of
        /// this function  is larger.
        ///
        ;
        ///
        /// # Example
        ///
        /// ```
        /// use abi_stable::std_types::RSliceMut;
        ///
        /// assert_eq!(
        ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3]).into_slice(),
        ///     &[0, 1, 2, 3]
        /// );
        ///
        /// ```
        pub fn into_slice(self) -> &'a [T] {
            unsafe { self.as_slice_unbounded_lifetime() }
        }
    }

    /// Creates an `RSlice<'_, T>` with access to all the elements of this slice.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::{RSlice, RSliceMut};
    ///
    /// assert_eq!(
    ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3]).as_rslice(),
    ///     RSlice::from_slice(&[0, 1, 2, 3]),
    /// );
    ///
    /// ```
    pub fn as_rslice(&self) -> RSlice<'_, T> {
        self.as_slice().into()
    }

    /// Creates an `RSlice<'a, T>` with access to all the elements of this slice.
    ///
    /// This is different to `as_rslice` in that the returned lifetime of
    /// this function  is larger.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::{RSlice, RSliceMut};
    ///
    /// assert_eq!(
    ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3]).into_rslice(),
    ///     RSlice::from_slice(&[0, 1, 2, 3]),
    /// );
    ///
    /// ```
    pub fn into_rslice(self) -> RSlice<'a, T> {
        self.into_slice().into()
    }

    /// Creates a `&'_ mut [T]` with access to all the elements of this slice.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// assert_eq!(
    ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3]).as_mut_slice(),
    ///     &mut [0, 1, 2, 3]
    /// );
    ///
    /// ```
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { self.as_mut_slice_unbounded_lifetime() }
    }

    /// Creates a `&'a mut [T]` with access to all the elements of this slice.
    ///
    /// This is different to `as_mut_slice` in that the returned lifetime of
    /// this function is larger.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::std_types::RSliceMut;
    ///
    /// assert_eq!(
    ///     RSliceMut::from_mut_slice(&mut [0, 1, 2, 3]).into_mut_slice(),
    ///     &mut [0, 1, 2, 3]
    /// );
    ///
    /// ```
    pub fn into_mut_slice(mut self) -> &'a mut [T] {
        unsafe { self.as_mut_slice_unbounded_lifetime() }
    }
}

unsafe impl<'a, T> Send for RSliceMut<'a, T> where &'a mut [T]: Send {}
unsafe impl<'a, T> Sync for RSliceMut<'a, T> where &'a mut [T]: Sync {}

impl<'a, T> Default for RSliceMut<'a, T> {
    fn default() -> Self {
        (&mut [][..]).into()
    }
}

impl<'a, T> IntoIterator for RSliceMut<'a, T> {
    type Item = &'a mut T;

    type IntoIter = ::std::slice::IterMut<'a, T>;

    fn into_iter(self) -> ::std::slice::IterMut<'a, T> {
        self.into_mut_slice().iter_mut()
    }
}

slice_like_impl_cmp_traits! {
    impl[] RSliceMut<'_, T>,
    where[];
    Vec<U>,
    [U],
    &[U],
    RSlice<'_, U>,
}

slice_like_impl_cmp_traits! {
    impl[const N: usize] RSliceMut<'_, T>,
    where[];
    [U; N],
}

slice_like_impl_cmp_traits! {
    impl[] RSliceMut<'_, T>,
    where[T: Clone, U: Clone];
    std::borrow::Cow<'_, [U]>,
    crate::std_types::RCowSlice<'_, U>,
}

impl<'a, T> Deref for RSliceMut<'a, T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<'a, T> DerefMut for RSliceMut<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

////////////////////////////

impl_into_rust_repr! {
    impl['a, T] Into<&'a mut [T]> for RSliceMut<'a, T> {
        fn(this){
            this.into_mut_slice()
        }
    }
}

impl<'a, T> From<RSliceMut<'a, T>> for &'a [T] {
    fn from(this: RSliceMut<'a, T>) -> &'a [T] {
        this.into_slice()
    }
}

////////////////////

impl<'a, T: 'a> Borrow<[T]> for RSliceMut<'a, T> {
    fn borrow(&self) -> &[T] {
        self
    }
}

impl<'a, T: 'a> BorrowMut<[T]> for RSliceMut<'a, T> {
    fn borrow_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<'a, T: 'a> AsRef<[T]> for RSliceMut<'a, T> {
    fn as_ref(&self) -> &[T] {
        self
    }
}

impl<'a, T: 'a> AsMut<[T]> for RSliceMut<'a, T> {
    fn as_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<'a, T, I: SliceIndex<[T]>> Index<I> for RSliceMut<'a, T> {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        Index::index(&**self, index)
    }
}

impl<'a, T, I: SliceIndex<[T]>> IndexMut<I> for RSliceMut<'a, T> {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        IndexMut::index_mut(&mut **self, index)
    }
}

////////////////////////////
impl<'a, T> Serialize for RSliceMut<'a, T>
where
    T: Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.as_slice().serialize(serializer)
    }
}

///////////////////////////////////////////////////////////////////////////////

impl<'a> Write for RSliceMut<'a, u8> {
    #[inline]
    fn write(&mut self, data: &[u8]) -> io::Result<usize> {
        let mut this = mem::take(self).into_mut_slice();
        let ret = this.write(data);
        *self = this.into();
        ret
    }

    #[inline]
    fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
        let mut this = mem::take(self).into_mut_slice();
        let ret = this.write_all(data);
        *self = this.into();
        ret
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

///////////////////////////////////////////////////////////////////////////////

#[allow(dead_code)]
type SliceMut<'a, T> = &'a mut [T];

shared_impls! {
    mod = slice_impls
    new_type = RSliceMut['a][T],
    original_type = SliceMut,
}

////////////////////////////////////////////////////////////////////////////////

//#[cfg(test)]
#[cfg(all(test, not(feature = "only_new_tests")))]
mod test {
    use super::*;

    #[test]
    fn from_to_slice() {
        let a = b"what the hell".to_vec();
        let mut a_clone = a.clone();
        let a_addr = a_clone.as_ptr();
        let mut b = RSliceMut::from(&mut a_clone[..]);

        assert_eq!(&*a, &*b);
        assert_eq!(&*a, &mut *b);
        assert_eq!(a_addr, b.data());
        assert_eq!(a.len(), b.len());
    }

    #[cfg(feature = "rust_1_64")]
    #[test]
    fn const_as_slice_test() {
        const RSM: RSliceMut<'_, u8> =
            unsafe { RSliceMut::from_raw_parts_mut(std::ptr::NonNull::dangling().as_ptr(), 0) };

        const SLICE_A: &[u8] = RSM.as_slice();
        const SLICE_B: &[u8] = RSM.into_slice();

        assert_eq!(SLICE_A, [0u8; 0]);
        assert_eq!(SLICE_B, [0u8; 0]);
    }

    #[test]
    fn test_index() {
        let mut v = vec![1, 2, 3, 4, 5];
        let s = RSliceMut::from_mut_slice(&mut v[..]);

        assert_eq!(s.index(0), &1);
        assert_eq!(s.index(4), &5);
        assert_eq!(s.index(..2), &mut [1, 2]);
        assert_eq!(s.index(1..2), &mut [2]);
        assert_eq!(s.index(3..), &mut [4, 5]);
    }

    #[test]
    fn test_index_mut() {
        let mut v = vec![1, 2, 3, 4, 5];
        let mut s = RSliceMut::from_mut_slice(&mut v[..]);

        assert_eq!(s.index_mut(0), &mut 1);
        assert_eq!(s.index_mut(4), &mut 5);
        assert_eq!(s.index_mut(..2), &mut [1, 2]);
        assert_eq!(s.index_mut(1..2), &mut [2]);
        assert_eq!(s.index_mut(3..), &mut [4, 5]);
    }
}