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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! This crate provides reference-counted slices that support easy subdivision.

#![no_std]
#![deny(unsafe_code)]

extern crate alloc;

use alloc::rc::Rc;
use alloc::sync::Arc;
use core::ops::Deref;

/// A read-only view into an underlying reference-counted slice.
///
/// The associated functions provided for this type do not take a receiver to avoid conflicting
/// with (present or future) methods on `[T]`, since `RcSlice<T>: Deref<Target = [T]>`.
pub struct RcSlice<T> {
    underlying: Rc<[T]>,
    start: usize,
    end: usize,
}

impl<T> Clone for RcSlice<T> {
    fn clone(&self) -> Self {
        Self {
            underlying: self.underlying.clone(),
            start: self.start,
            end: self.end,
        }
    }
}

impl<T> AsRef<[T]> for RcSlice<T> {
    fn as_ref(&self) -> &[T] {
        &self.underlying[self.start..self.end]
    }
}

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

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl<T> From<Rc<[T]>> for RcSlice<T> {
    fn from(underlying: Rc<[T]>) -> Self {
        let end = underlying.len();

        Self {
            underlying,
            start: 0,
            end,
        }
    }
}

impl<T> RcSlice<T> {
    /// Returns the starting and ending indices of the view `it` within the underlying slice.
    pub fn bounds(it: &Self) -> (usize, usize) {
        (it.start, it.end)
    }

    /// Increases the starting index of `it` by `incr` places, and returns a reference to the
    /// elements cut off by this operation.
    ///
    /// Returns `None` and leaves `it` unchanged if this operation would make the starting index
    /// greater than the ending index.
    pub fn advance(it: &mut Self, incr: usize) -> Option<&[T]> {
        let cut = it.start.checked_add(incr)?;

        if cut <= it.end {
            let shed = &it.underlying[it.start..cut];
            it.start = cut;

            Some(shed)
        } else {
            None
        }
    }

    /// Decreases the ending index of `it` by `decr` places, and returns a reference to the
    /// elements cut off by this operation.
    ///
    /// Returns `None` and leaves `it` unchanged if this operation would make the ending index less
    /// than the starting index.
    pub fn retract(it: &mut Self, decr: usize) -> Option<&[T]> {
        let cut = it.end.checked_sub(decr)?;

        if cut >= it.start {
            let shed = &it.underlying[cut..it.end];
            it.end = cut;

            Some(shed)
        } else {
            None
        }
    }

    /// Mutates the view `it` to point to only the first `index` elements of the underlying slice,
    /// and returns a new view of the remaining elements.
    ///
    /// Returns `None` and leaves `it` unchanged if the underlying slice has fewer than `index`
    /// elements.
    pub fn split_off_before(it: &mut Self, index: usize) -> Option<Self> {
        let cut = it.start.checked_add(index)?;

        if cut <= it.end {
            let mut front = it.clone();
            front.end = cut;
            it.start = cut;

            Some(front)
        } else {
            None
        }
    }

    /// Returns a new view of the first `index` elements of the underlying slice, and mutates `it`
    /// to point to only the remaining elements.
    ///
    /// Returns `None` and leaves `it` unchanged if the underlying slice has fewer than `index`
    /// elements.
    pub fn split_off_after(it: &mut Self, index: usize) -> Option<Self> {
        let cut = it.start.checked_add(index)?;

        if cut <= it.end {
            let mut back = it.clone();
            back.start = cut;
            it.end = cut;

            Some(back)
        } else {
            None
        }
    }
}

/// A read-only view into an underlying atomically reference-counted slice.
///
/// The associated functions provided for this type do not take a receiver to avoid conflicting
/// with (present or future) methods on `[T]`, since `ArcSlice<T>: Deref<Target = [T]>`.
pub struct ArcSlice<T> {
    underlying: Arc<[T]>,
    start: usize,
    end: usize,
}

impl<T> Clone for ArcSlice<T> {
    fn clone(&self) -> Self {
        Self {
            underlying: self.underlying.clone(),
            start: self.start,
            end: self.end,
        }
    }
}

impl<T> AsRef<[T]> for ArcSlice<T> {
    fn as_ref(&self) -> &[T] {
        &self.underlying[self.start..self.end]
    }
}

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

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl<T> From<Arc<[T]>> for ArcSlice<T> {
    fn from(underlying: Arc<[T]>) -> Self {
        let end = underlying.len();

        Self {
            underlying,
            start: 0,
            end,
        }
    }
}

impl<T> ArcSlice<T> {
    /// Returns the starting and ending indices of the view `it` within the underlying slice.
    pub fn bounds(it: &Self) -> (usize, usize) {
        (it.start, it.end)
    }

    /// Increases the starting index of `it` by `incr` places, and returns a reference to the
    /// elements cut off by this operation.
    ///
    /// Returns `None` and leaves `it` unchanged if this operation would make the starting index
    /// greater than the ending index.
    pub fn advance(it: &mut Self, incr: usize) -> Option<&[T]> {
        let cut = it.start.checked_add(incr)?;

        if cut <= it.end {
            let shed = &it.underlying[it.start..cut];
            it.start = cut;

            Some(shed)
        } else {
            None
        }
    }

    /// Decreases the ending index of `it` by `decr` places, and returns a reference to the
    /// elements cut off by this operation.
    ///
    /// Returns `None` and leaves `it` unchanged if this operation would make the ending index less
    /// than the starting index.
    pub fn retract(it: &mut Self, decr: usize) -> Option<&[T]> {
        let cut = it.end.checked_sub(decr)?;

        if cut >= it.start {
            let shed = &it.underlying[cut..it.end];
            it.end = cut;

            Some(shed)
        } else {
            None
        }
    }

    /// Mutates the view `it` to point to only the first `index` elements of the underlying slice,
    /// and returns a new view of the remaining elements.
    ///
    /// Returns `None` and leaves `it` unchanged if the underlying slice has fewer than `index`
    /// elements.
    pub fn split_off_before(it: &mut Self, index: usize) -> Option<Self> {
        let cut = it.start.checked_add(index)?;

        if cut <= it.end {
            let mut front = it.clone();
            front.end = cut;
            it.start = cut;

            Some(front)
        } else {
            None
        }
    }

    /// Returns a new view of the first `index` elements of the underlying slice, and mutates `it`
    /// to point to only the remaining elements.
    ///
    /// Returns `None` and leaves `it` unchanged if the underlying slice has fewer than `index`
    /// elements.
    pub fn split_off_after(it: &mut Self, index: usize) -> Option<Self> {
        let cut = it.start.checked_add(index)?;

        if cut <= it.end {
            let mut back = it.clone();
            back.start = cut;
            it.end = cut;

            Some(back)
        } else {
            None
        }
    }
}

pub type RcBytes = RcSlice<u8>;

pub type ArcBytes = ArcSlice<u8>;