Skip to content

Commit

Permalink
Merge pull request #29 from orxfun/index-and-index-mut-are-required
Browse files Browse the repository at this point in the history
Index and IndexMut traits are required
  • Loading branch information
orxfun authored Aug 22, 2024
2 parents cca9e3a + 7b6d805 commit 968f251
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "orx-pinned-vec"
version = "3.4.0"
version = "3.5.0"
edition = "2021"
authors = ["orxfun <[email protected]>"]
description = "`PinnedVec` trait defines the interface for vectors which guarantee that elements added to the vector are pinned to their memory locations unless explicitly changed."
Expand Down
9 changes: 7 additions & 2 deletions src/pinned_vec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::CapacityState;
use orx_pseudo_default::PseudoDefault;
use std::{cmp::Ordering, ops::RangeBounds};
use std::{
cmp::Ordering,
ops::{Index, IndexMut, RangeBounds},
};

/// Trait for vector representations differing from `std::vec::Vec` by the following:
///
Expand All @@ -27,7 +30,9 @@ use std::{cmp::Ordering, ops::RangeBounds};
/// | `pop()` | does not change the memory locations of the first `n-1` elements, the `n`-th element is removed |
/// | `remove(a)` | does not change the memory locations of the first `a` elements, where `a < n`; elements to the right of the removed element might be changed, commonly shifted to left |
/// | `truncate(a)` | does not change the memory locations of the first `a` elements, where `a < n` |
pub trait PinnedVec<T>: IntoIterator<Item = T> + PseudoDefault {
pub trait PinnedVec<T>:
IntoIterator<Item = T> + PseudoDefault + Index<usize, Output = T> + IndexMut<usize, Output = T>
{
/// Iterator yielding references to the elements of the vector.
type Iter<'a>: Iterator<Item = &'a T>
where
Expand Down
20 changes: 19 additions & 1 deletion src/pinned_vec_tests/test_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ mod tests {
CapacityState,
};
use orx_pseudo_default::PseudoDefault;
use std::{cmp::Ordering, iter::Rev, ops::RangeBounds};
use std::{
cmp::Ordering,
iter::Rev,
ops::{Index, IndexMut, RangeBounds},
};

#[derive(Debug)]
struct JustVec<T>(Vec<T>);
Expand All @@ -42,6 +46,20 @@ mod tests {
}
}

impl<T> Index<usize> for JustVec<T> {
type Output = T;

fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}

impl<T> IndexMut<usize> for JustVec<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.0[index]
}
}

impl<T> JustVec<T> {
fn assert_has_room(&self, required_additional_space: usize) {
assert!(PinnedVec::len(self) + required_additional_space <= self.0.capacity())
Expand Down
20 changes: 19 additions & 1 deletion src/pinned_vec_tests/testvec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use super::helpers::range::{range_end, range_start};
use crate::*;
use orx_pseudo_default::PseudoDefault;
use std::{cmp::Ordering, iter::Rev, ops::RangeBounds};
use std::{
cmp::Ordering,
iter::Rev,
ops::{Index, IndexMut, RangeBounds},
};

pub struct TestVec<T>(Vec<T>);

Expand All @@ -22,6 +26,20 @@ impl<T> TestVec<T> {
}
}

impl<T> Index<usize> for TestVec<T> {
type Output = T;

fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}

impl<T> IndexMut<usize> for TestVec<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.0[index]
}
}

impl<T> IntoIterator for TestVec<T> {
type Item = T;
type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
Expand Down

0 comments on commit 968f251

Please sign in to comment.