From 7b6d805c47b5537afcb09d36c712b348fbc277e5 Mon Sep 17 00:00:00 2001 From: orxfun Date: Thu, 22 Aug 2024 21:46:55 +0200 Subject: [PATCH] Index and IndexMut traits are required --- Cargo.toml | 2 +- src/pinned_vec.rs | 9 +++++++-- src/pinned_vec_tests/test_all.rs | 20 +++++++++++++++++++- src/pinned_vec_tests/testvec.rs | 20 +++++++++++++++++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ad3c191..6bd3e94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "orx-pinned-vec" -version = "3.4.0" +version = "3.5.0" edition = "2021" authors = ["orxfun "] 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." diff --git a/src/pinned_vec.rs b/src/pinned_vec.rs index 462fc49..a7e3c21 100644 --- a/src/pinned_vec.rs +++ b/src/pinned_vec.rs @@ -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: /// @@ -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: IntoIterator + PseudoDefault { +pub trait PinnedVec: + IntoIterator + PseudoDefault + Index + IndexMut +{ /// Iterator yielding references to the elements of the vector. type Iter<'a>: Iterator where diff --git a/src/pinned_vec_tests/test_all.rs b/src/pinned_vec_tests/test_all.rs index 7f143c9..7c6580a 100644 --- a/src/pinned_vec_tests/test_all.rs +++ b/src/pinned_vec_tests/test_all.rs @@ -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(Vec); @@ -42,6 +46,20 @@ mod tests { } } + impl Index for JustVec { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + &self.0[index] + } + } + + impl IndexMut for JustVec { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.0[index] + } + } + impl JustVec { fn assert_has_room(&self, required_additional_space: usize) { assert!(PinnedVec::len(self) + required_additional_space <= self.0.capacity()) diff --git a/src/pinned_vec_tests/testvec.rs b/src/pinned_vec_tests/testvec.rs index 0c53df2..c1fab20 100644 --- a/src/pinned_vec_tests/testvec.rs +++ b/src/pinned_vec_tests/testvec.rs @@ -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(Vec); @@ -22,6 +26,20 @@ impl TestVec { } } +impl Index for TestVec { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + &self.0[index] + } +} + +impl IndexMut for TestVec { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.0[index] + } +} + impl IntoIterator for TestVec { type Item = T; type IntoIter = as IntoIterator>::IntoIter;