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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use core::ops::Range;

use crate::ptr_utils::*;

/// Represents an interval of memory `[base, acme)`
///
/// Use `get_base_acme` to retrieve `base` and `acme` directly.
///
/// # Empty Spans
/// Note that where `base >= acme`, the [`Span`] is considered empty, in which case
/// the specific values of `base` and `acme` are considered meaningless.
/// * Empty spans contain nothing and overlap with nothing.
/// * Empty spans are contained by any sized span.
#[derive(Clone, Copy, Hash)]
pub struct Span {
    base: *mut u8,
    acme: *mut u8,
}

unsafe impl Send for Span {}

impl Default for Span {
    fn default() -> Self {
        Self::empty()
    }
}

impl core::fmt::Debug for Span {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("{:p}..[{}]..{:p}", self.base, self.size(), self.acme))
    }
}

impl core::fmt::Display for Span {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self.get_base_acme() {
            Some((base, acme)) => f.write_fmt(format_args!("{:p}..{:p}", base, acme)),
            None => f.write_str("Empty Span"),
        }
    }
}

impl<T> From<Range<*mut T>> for Span {
    fn from(value: Range<*mut T>) -> Self {
        Self { base: value.start.cast(), acme: value.end.cast() }
    }
}

impl<T> From<Range<*const T>> for Span {
    fn from(value: Range<*const T>) -> Self {
        Self { base: value.start.cast_mut().cast(), acme: value.end.cast_mut().cast() }
    }
}

impl<T> From<&mut [T]> for Span {
    fn from(value: &mut [T]) -> Self {
        Self::from(value.as_mut_ptr_range())
    }
}

impl<T> From<&[T]> for Span {
    fn from(value: &[T]) -> Self {
        Self::from(value.as_ptr_range())
    }
}

impl<T, const N: usize> From<&mut [T; N]> for Span {
    fn from(value: &mut [T; N]) -> Self {
        Self::from(value as *mut [T; N])
    }
}

impl<T, const N: usize> From<&[T; N]> for Span {
    fn from(value: &[T; N]) -> Self {
        Self::from(value as *const [T; N])
    }
}

#[cfg(feature = "nightly_api")]
impl<T> From<*mut [T]> for Span {
    fn from(value: *mut [T]) -> Self {
        Self::from_slice(value)
    }
}

#[cfg(feature = "nightly_api")]
impl<T> From<*const [T]> for Span {
    fn from(value: *const [T]) -> Self {
        Self::from_const_slice(value)
    }
}

impl<T, const N: usize> From<*mut [T; N]> for Span {
    fn from(value: *mut [T; N]) -> Self {
        Self::from_array(value)
    }
}

impl<T, const N: usize> From<*const [T; N]> for Span {
    fn from(value: *const [T; N]) -> Self {
        Self::from_array(value.cast_mut())
    }
}

impl PartialEq for Span {
    fn eq(&self, other: &Self) -> bool {
        self.is_empty() && other.is_empty() || self.base == other.base && self.acme == other.acme
    }
}
impl Eq for Span {}

impl Span {
    /// Returns whether `base >= acme`.
    #[inline]
    pub fn is_empty(self) -> bool {
        self.acme <= self.base
    }

    /// Returns whether `base < acme`.
    #[inline]
    pub fn is_sized(self) -> bool {
        !self.is_empty()
    }

    /// Returns the size of the span, else zero if `base >= span`.
    #[inline]
    pub fn size(self) -> usize {
        if self.is_empty() { 0 } else { self.acme as usize - self.base as usize }
    }

    /// If `self` isn't empty, returns `(base, acme)`
    #[inline]
    pub fn get_base_acme(self) -> Option<(*mut u8, *mut u8)> {
        if self.is_empty() { None } else { Some((self.base, self.acme)) }
    }

    /// Create an empty span.
    #[inline]
    pub const fn empty() -> Self {
        Self { base: core::ptr::null_mut(), acme: core::ptr::null_mut() }
    }

    /// Create a new span.
    #[inline]
    pub const fn new(base: *mut u8, acme: *mut u8) -> Self {
        Self { base, acme }
    }

    /// Creates a [`Span`] given a `base` and a `size`.
    ///
    /// If `base + size` overflows, the result is empty.
    #[inline]
    pub const fn from_base_size(base: *mut u8, size: usize) -> Self {
        Self { base, acme: base.wrapping_add(size) }
    }

    #[cfg(feature = "nightly_api")]
    #[inline]
    pub const fn from_slice<T>(slice: *mut [T]) -> Self {
        Self {
            base: slice as *mut T as *mut u8,
            // SAFETY: pointing directly after an object is considered
            // within the same object
            acme: unsafe { (slice as *mut T).add(slice.len()).cast() },
        }
    }

    #[cfg(feature = "nightly_api")]
    #[inline]
    pub const fn from_const_slice<T>(slice: *const [T]) -> Self {
        Self {
            base: slice as *mut T as *mut u8,
            // SAFETY: pointing directly after an object is considered
            // within the same object
            acme: unsafe { (slice as *mut T).add(slice.len()).cast() },
        }
    }

    #[inline]
    pub const fn from_array<T, const N: usize>(array: *mut [T; N]) -> Self {
        Self {
            base: array as *mut T as *mut u8,
            // SAFETY: pointing directly after an object is considered
            // within the same object
            acme: unsafe { (array as *mut T).add(N).cast() },
        }
    }

    #[inline]
    pub const fn from_const_array<T, const N: usize>(array: *const [T; N]) -> Self {
        Self {
            base: array as *mut T as *mut u8,
            // SAFETY: pointing directly after an object is considered
            // within the same object
            acme: unsafe { (array as *mut T).add(N).cast() },
        }
    }

    /// Returns `None` if `self` is empty.
    #[inline]
    pub fn to_ptr_range(self) -> Option<Range<*mut u8>> {
        if self.is_empty() { None } else { Some(self.base..self.acme) }
    }

    /// Returns `None` if `self` is empty.
    #[inline]
    pub fn to_slice(self) -> Option<*mut [u8]> {
        if self.is_empty() {
            None
        } else {
            Some(core::ptr::slice_from_raw_parts_mut(self.base, self.size()))
        }
    }

    /// Returns whether `self` contains `addr`.
    ///
    /// Empty spans contain nothing.
    #[inline]
    pub fn contains(self, ptr: *mut u8) -> bool {
        // if self is empty, this always evaluates to false
        self.base <= ptr && ptr < self.acme
    }

    /// Returns whether `self` contains `other`.
    ///
    /// Empty spans are contained by any span, even empty ones.
    #[inline]
    pub fn contains_span(self, other: Span) -> bool {
        other.is_empty() || self.base <= other.base && other.acme <= self.acme
    }

    /// Returns whether some of `self` overlaps with `other`.
    ///
    /// Empty spans don't overlap with anything.
    #[inline]
    pub fn overlaps(self, other: Span) -> bool {
        self.is_sized() && other.is_sized() && !(other.base >= self.acme || self.base >= other.acme)
    }

    /// Aligns `base` upward and `acme` downward by `align_of::<usize>()`.
    #[inline]
    pub fn word_align_inward(self) -> Self {
        if ALIGN > usize::MAX - self.base as usize {
            Self::empty()
        } else {
            Self { base: align_up(self.base), acme: align_down(self.acme) }
        }
    }
    /// Aligns `base` downward and `acme` upward by `align_of::<usize>()`.
    #[inline]
    pub fn word_align_outward(self) -> Self {
        if ALIGN > usize::MAX - self.acme as usize {
            panic!("aligning acme upward would overflow!");
        }

        Self { base: align_down(self.base), acme: align_up(self.acme) }
    }

    /// Raises `base` if `base` is smaller than `min`.
    #[inline]
    pub fn above(self, min: *mut u8) -> Self {
        Self { base: if min > self.base { min } else { self.base }, acme: self.acme }
    }
    /// Lowers `acme` if `acme` is greater than `max`.
    #[inline]
    pub fn below(self, max: *mut u8) -> Self {
        Self { base: self.base, acme: if max < self.acme { max } else { self.acme } }
    }

    /// Returns the [`Span`]s of `self` below and above the `exclude` span, respectively.
    /// Alternatively worded, the set difference `self`\\`exclude`.
    /// 
    /// If `exclude` is empty, `self` and an empty `Span` are returned.
    #[inline]
    pub fn except(self, exclude: Span) -> (Self, Self) {
        match exclude.get_base_acme() {
            Some((base, acme)) => (self.below(base), self.above(acme)),
            None => (self, Span::empty()),
        }
    }

    /// Returns a span that `other` contains by raising `base` or lowering `acme`.
    ///
    /// If `other` is empty, returns `other`.
    #[inline]
    pub fn fit_within(self, other: Span) -> Self {
        if other.is_empty() {
            other
        } else {
            Self {
                base: if other.base > self.base { other.base } else { self.base },
                acme: if other.acme < self.acme { other.acme } else { self.acme },
            }
        }
    }
    /// Returns a span that contains `other` by extending `self`.
    ///
    /// If `other` is empty, returns `self`, as all spans contain any empty span.
    #[inline]
    pub fn fit_over(self, other: Self) -> Self {
        if other.is_empty() {
            self
        } else {
            Self {
                base: if other.base < self.base { other.base } else { self.base },
                acme: if other.acme > self.acme { other.acme } else { self.acme },
            }
        }
    }

    /// Lower `base` by `low` and raise `acme` by `high`.
    ///
    /// Does nothing if `self` is empty.
    ///
    /// # Panics
    /// Panics if lowering `base` by `low` or raising `acme` by `high` under/overflows.
    #[inline]
    pub fn extend(self, low: usize, high: usize) -> Self {
        if self.is_empty() {
            self
        } else {
            assert!((self.base as usize).checked_sub(low).is_some());
            assert!((self.acme as usize).checked_add(high).is_some());

            Self { base: self.base.wrapping_sub(low), acme: self.acme.wrapping_add(high) }
        }
    }

    /// Raise `base` by `low` and lower `acme` by `high`.
    ///
    /// If `self` is empty, `self` is returned.
    ///
    /// If either operation would wrap around the address space, an empty span is returned.
    #[inline]
    pub fn truncate(self, low: usize, high: usize) -> Span {
        if self.is_empty() {
            self
        } else if (self.base as usize).checked_add(low).is_none()
            || (self.acme as usize).checked_sub(high).is_none()
        {
            Span::empty()
        } else {
            Self {
                // if either boundary saturates, the span will be empty thereafter, as expected
                base: self.base.wrapping_add(low),
                acme: self.acme.wrapping_sub(high),
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn ptr(addr: usize) -> *mut u8 {
        // don't ` as usize` to avoid upsetting miri too much
        core::ptr::null_mut::<u8>().wrapping_add(addr)
    }

    #[test]
    fn test_span() {
        let base = 1234usize;
        let acme = 5678usize;

        let bptr = ptr(base);
        let aptr = ptr(acme);

        let span = Span::from(bptr..aptr);
        assert!(!span.is_empty());
        assert!(span.size() == acme - base);

        assert!(
            span.word_align_inward()
                == Span::new(
                    bptr.wrapping_add(ALIGN - 1)
                        .wrapping_sub(bptr.wrapping_add(ALIGN - 1) as usize & (ALIGN - 1)),
                    aptr.wrapping_sub(acme & (ALIGN - 1))
                )
        );
        assert!(
            span.word_align_outward()
                == Span::new(
                    bptr.wrapping_sub(base & (ALIGN - 1)),
                    aptr.wrapping_add(ALIGN - 1)
                        .wrapping_sub(aptr.wrapping_add(ALIGN - 1) as usize & (ALIGN - 1))
                )
        );

        assert_eq!(span.above(ptr(2345)), Span::new(ptr(2345), aptr));
        assert_eq!(span.below(ptr(7890)), Span::new(bptr, aptr));
        assert_eq!(span.below(ptr(3456)), Span::new(bptr, ptr(3456)));
        assert_eq!(span.below(ptr(0123)), Span::empty());
        assert_eq!(span.above(ptr(7890)), Span::empty());

        assert_eq!(
            span.except(Span::new(ptr(base + 1111), ptr(acme - 1111))),
            (Span::new(bptr, ptr(base + 1111)), Span::new(ptr(acme - 1111), aptr))
        );
        assert_eq!(
            span.except(Span::new(ptr(base + 1111), ptr(acme + 1111))),
            (Span::new(bptr, ptr(base + 1111)), Span::empty())
        );
        assert_eq!(
            span.except(Span::new(ptr(base - 1111), ptr(acme + 1111))),
            (Span::empty(), Span::empty())
        );
        assert_eq!(span.except(Span::empty()), (span, Span::empty()));

        assert!(span.fit_over(Span::empty()) == span);
        assert!(span.fit_within(Span::empty()).is_empty());
        assert!(span.fit_within(Span::new(ptr(0), ptr(10000))) == span);
        assert!(span.fit_over(Span::new(ptr(0), ptr(10000))) == Span::new(ptr(0), ptr(10000)));
        assert!(span.fit_within(Span::new(ptr(4000), ptr(10000))) == Span::new(ptr(4000), aptr));
        assert!(span.fit_over(Span::new(ptr(4000), ptr(10000))) == Span::new(bptr, ptr(10000)));

        assert!(span.extend(1234, 1010) == Span::new(ptr(0), ptr(5678 + 1010)));
        assert!(span.truncate(1234, 1010) == Span::new(ptr(1234 + 1234), ptr(5678 - 1010)));
        assert!(span.truncate(235623, 45235772).is_empty());
    }
}