forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththin_slice.rs
More file actions
53 lines (44 loc) · 1.1 KB
/
thin_slice.rs
File metadata and controls
53 lines (44 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::ops::{Deref, DerefMut};
use thin_slice::ThinBoxedSlice;
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct ThinSlice<T> {
slice: ThinBoxedSlice<T>,
}
impl<T> ThinSlice<T> {
pub fn into_vec(self) -> Vec<T> {
self.into()
}
}
impl<T> Default for ThinSlice<T> {
fn default() -> Self {
Self { slice: Default::default() }
}
}
impl<T> From<Vec<T>> for ThinSlice<T> {
fn from(vec: Vec<T>) -> Self {
Self { slice: vec.into_boxed_slice2().into() }
}
}
impl<T> From<ThinSlice<T>> for Vec<T> {
fn from(slice: ThinSlice<T>) -> Self {
let boxed: Box<[T]> = slice.slice.into();
boxed.into_vec()
}
}
impl<T> FromIterator<T> for ThinSlice<T> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
let vec: Vec<T> = iter.into_iter().collect();
vec.into()
}
}
impl<T> Deref for ThinSlice<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
self.slice.deref()
}
}
impl<T> DerefMut for ThinSlice<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.slice.deref_mut()
}
}