File size: 2,053 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::{
    borrow::Borrow,
    fmt::{self, Debug, Formatter},
    hash::{Hash, Hasher},
    ops::{Deref, Range},
    sync::Arc,
};

/// A owned slice that is backed by an `Arc`.
#[derive(Clone)]
pub struct ArcSlice<T> {
    data: *const [T],
    arc: Arc<[T]>,
}

unsafe impl<T> Send for ArcSlice<T> {}
unsafe impl<T> Sync for ArcSlice<T> {}

impl<T> From<Arc<[T]>> for ArcSlice<T> {
    fn from(arc: Arc<[T]>) -> Self {
        Self {
            data: &*arc as *const [T],
            arc,
        }
    }
}

impl<T> From<Box<[T]>> for ArcSlice<T> {
    fn from(b: Box<[T]>) -> Self {
        Self::from(Arc::from(b))
    }
}

impl<T> Deref for ArcSlice<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.data }
    }
}

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

impl<T: Hash> Hash for ArcSlice<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.deref().hash(state)
    }
}

impl<T: PartialEq> PartialEq for ArcSlice<T> {
    fn eq(&self, other: &Self) -> bool {
        self.deref().eq(other.deref())
    }
}

impl<T: Debug> Debug for ArcSlice<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Debug::fmt(&**self, f)
    }
}

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

impl<T> ArcSlice<T> {
    /// Creates a new `ArcSlice` from a pointer to a slice and an `Arc`.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the pointer is pointing to a valid slice that is kept alive by
    /// the `Arc`.
    pub unsafe fn new_unchecked(data: *const [T], arc: Arc<[T]>) -> Self {
        Self { data, arc }
    }

    /// Get the backing arc
    pub fn full_arc(this: &ArcSlice<T>) -> Arc<[T]> {
        this.arc.clone()
    }

    /// Returns a new `ArcSlice` that points to a slice of the current slice.
    pub fn slice(self, range: Range<usize>) -> ArcSlice<T> {
        let data = &*self;
        let data = &data[range] as *const [T];
        Self {
            data,
            arc: self.arc,
        }
    }
}