|
| 1 | +use std::{borrow::Borrow, ops::Deref}; |
| 2 | + |
| 3 | +// Use our fake Send/Sync traits when on not parallel compiler, |
| 4 | +// so that `OwnedSlice` only implements/requires Send/Sync |
| 5 | +// for parallel compiler builds. |
| 6 | +use crate::sync::{Send, Sync}; |
| 7 | + |
| 8 | +/// An owned slice. |
| 9 | +/// |
| 10 | +/// This is similar to `Box<[u8]>` but allows slicing and using anything as the |
| 11 | +/// backing buffer. |
| 12 | +/// |
| 13 | +/// See [`slice_owned`] for `OwnedSlice` construction and examples. |
| 14 | +/// |
| 15 | +/// --------------------------------------------------------------------------- |
| 16 | +/// |
| 17 | +/// This is essentially a replacement for `owning_ref` which is a lot simpler |
| 18 | +/// and even sound! 🌸 |
| 19 | +pub struct OwnedSlice { |
| 20 | + /// This is conceptually a `&'self.owner [u8]`. |
| 21 | + bytes: *const [u8], |
| 22 | + |
| 23 | + // +---------------------------------------+ |
| 24 | + // | We expect `dead_code` lint here, | |
| 25 | + // | because we don't want to accidentally | |
| 26 | + // | touch the owner — otherwise the owner | |
| 27 | + // | could invalidate out `bytes` pointer | |
| 28 | + // | | |
| 29 | + // | so be quiet | |
| 30 | + // +----+ +-------------------------------+ |
| 31 | + // \/ |
| 32 | + // ⊂(´・◡・⊂ )∘˚˳° (I am the phantom remnant of #97770) |
| 33 | + #[expect(dead_code)] |
| 34 | + owner: Box<dyn Send + Sync>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Makes an [`OwnedSlice`] out of an `owner` and a `slicer` function. |
| 38 | +/// |
| 39 | +/// ## Examples |
| 40 | +/// |
| 41 | +/// ```rust |
| 42 | +/// # use rustc_data_structures::owned_slice::{OwnedSlice, slice_owned}; |
| 43 | +/// let vec = vec![1, 2, 3, 4]; |
| 44 | +/// |
| 45 | +/// // Identical to slicing via `&v[1..3]` but produces an owned slice |
| 46 | +/// let slice: OwnedSlice = slice_owned(vec, |v| &v[1..3]); |
| 47 | +/// assert_eq!(&*slice, [2, 3]); |
| 48 | +/// ``` |
| 49 | +/// |
| 50 | +/// ```rust |
| 51 | +/// # use rustc_data_structures::owned_slice::{OwnedSlice, slice_owned}; |
| 52 | +/// # use std::ops::Deref; |
| 53 | +/// let vec = vec![1, 2, 3, 4]; |
| 54 | +/// |
| 55 | +/// // Identical to slicing via `&v[..]` but produces an owned slice |
| 56 | +/// let slice: OwnedSlice = slice_owned(vec, Deref::deref); |
| 57 | +/// assert_eq!(&*slice, [1, 2, 3, 4]); |
| 58 | +/// ``` |
| 59 | +pub fn slice_owned<O, F>(owner: O, slicer: F) -> OwnedSlice |
| 60 | +where |
| 61 | + O: Send + Sync + 'static, |
| 62 | + F: FnOnce(&O) -> &[u8], |
| 63 | +{ |
| 64 | + try_slice_owned(owner, |x| Ok::<_, !>(slicer(x))).into_ok() |
| 65 | +} |
| 66 | + |
| 67 | +/// Makes an [`OwnedSlice`] out of an `owner` and a `slicer` function that can fail. |
| 68 | +/// |
| 69 | +/// See [`slice_owned`] for the infallible version. |
| 70 | +pub fn try_slice_owned<O, F, E>(owner: O, slicer: F) -> Result<OwnedSlice, E> |
| 71 | +where |
| 72 | + O: Send + Sync + 'static, |
| 73 | + F: FnOnce(&O) -> Result<&[u8], E>, |
| 74 | +{ |
| 75 | + // We box the owner of the bytes, so it doesn't move. |
| 76 | + // |
| 77 | + // Since the owner does not move and we don't access it in any way |
| 78 | + // before drop, there is nothing that can invalidate the bytes pointer. |
| 79 | + // |
| 80 | + // Thus, "extending" the lifetime of the reference returned from `F` is fine. |
| 81 | + // We pretend that we pass it a reference that lives as long as the returned slice. |
| 82 | + // |
| 83 | + // N.B. the HRTB on the `slicer` is important — without it the caller could provide |
| 84 | + // a short lived slice, unrelated to the owner. |
| 85 | + |
| 86 | + let owner = Box::new(owner); |
| 87 | + let bytes = slicer(&*owner)?; |
| 88 | + |
| 89 | + Ok(OwnedSlice { bytes, owner }) |
| 90 | +} |
| 91 | + |
| 92 | +impl Deref for OwnedSlice { |
| 93 | + type Target = [u8]; |
| 94 | + |
| 95 | + #[inline] |
| 96 | + fn deref(&self) -> &[u8] { |
| 97 | + // Safety: |
| 98 | + // `self.bytes` is valid per the construction in `slice_owned` |
| 99 | + // (which is the only constructor) |
| 100 | + unsafe { &*self.bytes } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Borrow<[u8]> for OwnedSlice { |
| 105 | + #[inline] |
| 106 | + fn borrow(&self) -> &[u8] { |
| 107 | + self |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Safety: `OwnedSlice` is conceptually `(&'self.1 [u8], Box<dyn Send + Sync>)`, which is `Send` |
| 112 | +unsafe impl Send for OwnedSlice {} |
| 113 | + |
| 114 | +// Safety: `OwnedSlice` is conceptually `(&'self.1 [u8], Box<dyn Send + Sync>)`, which is `Sync` |
| 115 | +unsafe impl Sync for OwnedSlice {} |
| 116 | + |
| 117 | +#[cfg(test)] |
| 118 | +mod tests; |
0 commit comments