Skip to content

Commit

Permalink
Implementing the following traits for Vec32<T>: AsMut<[T]>, AsMut<Vec…
Browse files Browse the repository at this point in the history
…32<T>>, AsRef<[T]>, AsRef<Vec32<T>>, Borrow<[T]>, BorrowMut<[T]>, Default, Index, IndexMut, and Ord.
  • Loading branch information
ryanavella authored and mbrubeck committed Nov 17, 2020
1 parent a0eae36 commit 74a55b9
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/vec32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// copied, modified, or distributed except according to those terms.


use std::{cmp, fmt, iter, mem, ops, ptr, slice, u32, vec};
use std::{borrow, cmp, fmt, iter, mem, ops, ptr, slice, u32, vec};
use std::ptr::NonNull;

/// A vector that is indexed by `u32` instead of `usize`.
Expand Down Expand Up @@ -310,6 +310,48 @@ macro_rules! vec32 {

// Trait implementations:

impl<T> AsMut<Vec32<T>> for Vec32<T> {
fn as_mut(&mut self) -> &mut Vec32<T> {
self
}
}

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

impl<T> AsRef<Vec32<T>> for Vec32<T> {
fn as_ref(&self) -> &Vec32<T> {
self
}
}

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

impl<T> borrow::Borrow<[T]> for Vec32<T> {
fn borrow(&self) -> &[T] {
&self[..]
}
}

impl<T> borrow::BorrowMut<[T]> for Vec32<T> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut self[..]
}
}

impl<T> Default for Vec32<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> Drop for Vec32<T> {
fn drop(&mut self) {
unsafe {
Expand All @@ -319,6 +361,20 @@ impl<T> Drop for Vec32<T> {
}
}

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

fn index(&self, index: I) -> &Self::Output {
ops::Index::index(&**self, index)
}
}

impl<T, I: slice::SliceIndex<[T]>> ops::IndexMut<I> for Vec32<T> {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
ops::IndexMut::index_mut(&mut **self, index)
}
}

unsafe impl<T: Sync> Sync for Vec32<T> {}
unsafe impl<T: Send> Send for Vec32<T> {}

Expand Down Expand Up @@ -412,6 +468,12 @@ impl<T: PartialOrd> PartialOrd for Vec32<T> {
}
}

impl<T: Ord> Ord for Vec32<T> {
fn cmp(&self, other: &Vec32<T>) -> cmp::Ordering {
Ord::cmp(&**self, &**other)
}
}

impl<T: Eq> Eq for Vec32<T> {}

impl<T, U> PartialEq<U> for Vec32<T> where U: for<'a> PartialEq<&'a [T]> {
Expand Down

0 comments on commit 74a55b9

Please sign in to comment.