Skip to content

Commit

Permalink
impl AsRef, AsRefMut, Borrow, BorrowMut
Browse files Browse the repository at this point in the history
  • Loading branch information
whentze committed Apr 26, 2019
1 parent f09df3e commit c68c3a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/lib.rs
Expand Up @@ -90,6 +90,7 @@ version = "0.2.1"
*/

use std::{
borrow::{Borrow, BorrowMut},
cmp::{Eq, PartialEq},
fmt,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -368,6 +369,30 @@ impl<T> DerefMut for VecShard<T> {
}
}

impl<T> AsRef<[T]> for VecShard<T> {
fn as_ref(&self) -> &[T] {
&*self
}
}

impl<T> AsMut<[T]> for VecShard<T> {
fn as_mut(&mut self) -> &mut [T] {
&mut *self
}
}

impl<T> Borrow<[T]> for VecShard<T> {
fn borrow(&self) -> &[T] {
&*self
}
}

impl<T> BorrowMut<[T]> for VecShard<T> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut *self
}
}

impl<T, I: SliceIndex<[T]>> Index<I> for VecShard<T> {
type Output = <I as slice::SliceIndex<[T]>>::Output;

Expand Down
17 changes: 17 additions & 0 deletions tests/basic.rs
Expand Up @@ -257,6 +257,23 @@ fn lens_match_up() {
}
}

#[test]
fn borrow_schmorrow() {
use std::borrow::{Borrow, BorrowMut};

let mut s = VecShard::from(vec!["sag", "ich", "immer"]);
fn cast_schmast<T>(r: &[T]) -> (usize, usize) {
(r.as_ptr() as usize, r.len())
}

assert_eq!(cast_schmast(&*s), cast_schmast(&mut *s));
assert_eq!(cast_schmast(&*s), cast_schmast(&s[..]));
assert_eq!(cast_schmast(&*s), cast_schmast(s.as_ref()));
assert_eq!(cast_schmast(&*s), cast_schmast(s.as_mut()));
assert_eq!(cast_schmast(&*s), cast_schmast(s.borrow()));
assert_eq!(cast_schmast(&*s), cast_schmast(s.borrow_mut()));
}

#[test]
fn zst_elements() {
let (mut left, mut right) = vec![(); 30].split_inplace_at(15);
Expand Down

0 comments on commit c68c3a3

Please sign in to comment.