-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Moritz Hoffmann <[email protected]>
- Loading branch information
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pub mod slice; | |
pub mod slice_copy; | ||
pub mod string; | ||
pub mod tuple; | ||
mod vec; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! Definitions to use `Vec<T>` as a region. | ||
use crate::{Push, Region, ReserveItems}; | ||
|
||
impl<T: Clone> Region for Vec<T> { | ||
type Owned = T; | ||
type ReadItem<'a> = &'a T where Self: 'a; | ||
type Index = usize; | ||
|
||
fn merge_regions<'a>(regions: impl Iterator<Item = &'a Self> + Clone) -> Self | ||
where | ||
Self: 'a, | ||
{ | ||
Self::with_capacity(regions.map(Vec::len).sum()) | ||
} | ||
|
||
fn index(&self, index: Self::Index) -> Self::ReadItem<'_> { | ||
&self[index] | ||
} | ||
|
||
fn reserve_regions<'a, I>(&mut self, regions: I) | ||
where | ||
Self: 'a, | ||
I: Iterator<Item = &'a Self> + Clone, | ||
{ | ||
self.reserve(regions.map(Vec::len).sum()); | ||
} | ||
|
||
fn clear(&mut self) { | ||
self.clear(); | ||
} | ||
|
||
fn heap_size<F: FnMut(usize, usize)>(&self, mut callback: F) { | ||
let size_of_t = std::mem::size_of::<T>(); | ||
callback(self.len() * size_of_t, self.capacity() * size_of_t); | ||
} | ||
|
||
fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> | ||
where | ||
Self: 'a, | ||
{ | ||
item | ||
} | ||
} | ||
|
||
impl<T: Clone> Push<T> for Vec<T> { | ||
fn push(&mut self, item: T) -> Self::Index { | ||
self.push(item); | ||
self.len() - 1 | ||
} | ||
} | ||
|
||
impl<T: Clone> Push<&T> for Vec<T> { | ||
fn push(&mut self, item: &T) -> Self::Index { | ||
self.push(item.clone()); | ||
self.len() - 1 | ||
} | ||
} | ||
|
||
impl<T: Clone> Push<&&T> for Vec<T> { | ||
fn push(&mut self, item: &&T) -> Self::Index { | ||
self.push((*item).clone()); | ||
self.len() - 1 | ||
} | ||
} | ||
|
||
impl<T: Clone, D> ReserveItems<D> for Vec<T> { | ||
fn reserve_items<I>(&mut self, items: I) | ||
where | ||
I: Iterator<Item = D> + Clone, | ||
{ | ||
self.reserve(items.count()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn vec() { | ||
use crate::{Push, Region, ReserveItems}; | ||
|
||
let mut region = Vec::<u32>::new(); | ||
let index = <_ as Push<_>>::push(&mut region, 42); | ||
assert_eq!(region.index(index), &42); | ||
|
||
let mut region = Vec::<u32>::new(); | ||
region.push(42); | ||
region.push(43); | ||
region.push(44); | ||
region.reserve_items([1, 2, 3].iter()); | ||
assert_eq!(region.index(0), &42); | ||
assert_eq!(region.index(1), &43); | ||
assert_eq!(region.index(2), &44); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters