forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#109971 - WaffleLapkin:yeet_ownership, r=Nil…
…strieb Yeet `owning_ref` Based on the discussions from rust-lang#109948 This replaces `owning_ref` with a far simpler & safer abstraction. Fixes rust-lang#109974
- Loading branch information
Showing
13 changed files
with
227 additions
and
2,012 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use std::{borrow::Borrow, ops::Deref}; | ||
|
||
// Use our fake Send/Sync traits when on not parallel compiler, | ||
// so that `OwnedSlice` only implements/requires Send/Sync | ||
// for parallel compiler builds. | ||
use crate::sync::{Send, Sync}; | ||
|
||
/// An owned slice. | ||
/// | ||
/// This is similar to `Box<[u8]>` but allows slicing and using anything as the | ||
/// backing buffer. | ||
/// | ||
/// See [`slice_owned`] for `OwnedSlice` construction and examples. | ||
/// | ||
/// --------------------------------------------------------------------------- | ||
/// | ||
/// This is essentially a replacement for `owning_ref` which is a lot simpler | ||
/// and even sound! 🌸 | ||
pub struct OwnedSlice { | ||
/// This is conceptually a `&'self.owner [u8]`. | ||
bytes: *const [u8], | ||
|
||
// +---------------------------------------+ | ||
// | We expect `dead_code` lint here, | | ||
// | because we don't want to accidentally | | ||
// | touch the owner — otherwise the owner | | ||
// | could invalidate out `bytes` pointer | | ||
// | | | ||
// | so be quiet | | ||
// +----+ +-------------------------------+ | ||
// \/ | ||
// ⊂(´・◡・⊂ )∘˚˳° (I am the phantom remnant of #97770) | ||
#[expect(dead_code)] | ||
owner: Box<dyn Send + Sync>, | ||
} | ||
|
||
/// Makes an [`OwnedSlice`] out of an `owner` and a `slicer` function. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ```rust | ||
/// # use rustc_data_structures::owned_slice::{OwnedSlice, slice_owned}; | ||
/// let vec = vec![1, 2, 3, 4]; | ||
/// | ||
/// // Identical to slicing via `&v[1..3]` but produces an owned slice | ||
/// let slice: OwnedSlice = slice_owned(vec, |v| &v[1..3]); | ||
/// assert_eq!(&*slice, [2, 3]); | ||
/// ``` | ||
/// | ||
/// ```rust | ||
/// # use rustc_data_structures::owned_slice::{OwnedSlice, slice_owned}; | ||
/// # use std::ops::Deref; | ||
/// let vec = vec![1, 2, 3, 4]; | ||
/// | ||
/// // Identical to slicing via `&v[..]` but produces an owned slice | ||
/// let slice: OwnedSlice = slice_owned(vec, Deref::deref); | ||
/// assert_eq!(&*slice, [1, 2, 3, 4]); | ||
/// ``` | ||
pub fn slice_owned<O, F>(owner: O, slicer: F) -> OwnedSlice | ||
where | ||
O: Send + Sync + 'static, | ||
F: FnOnce(&O) -> &[u8], | ||
{ | ||
try_slice_owned(owner, |x| Ok::<_, !>(slicer(x))).into_ok() | ||
} | ||
|
||
/// Makes an [`OwnedSlice`] out of an `owner` and a `slicer` function that can fail. | ||
/// | ||
/// See [`slice_owned`] for the infallible version. | ||
pub fn try_slice_owned<O, F, E>(owner: O, slicer: F) -> Result<OwnedSlice, E> | ||
where | ||
O: Send + Sync + 'static, | ||
F: FnOnce(&O) -> Result<&[u8], E>, | ||
{ | ||
// We box the owner of the bytes, so it doesn't move. | ||
// | ||
// Since the owner does not move and we don't access it in any way | ||
// before drop, there is nothing that can invalidate the bytes pointer. | ||
// | ||
// Thus, "extending" the lifetime of the reference returned from `F` is fine. | ||
// We pretend that we pass it a reference that lives as long as the returned slice. | ||
// | ||
// N.B. the HRTB on the `slicer` is important — without it the caller could provide | ||
// a short lived slice, unrelated to the owner. | ||
|
||
let owner = Box::new(owner); | ||
let bytes = slicer(&*owner)?; | ||
|
||
Ok(OwnedSlice { bytes, owner }) | ||
} | ||
|
||
impl Deref for OwnedSlice { | ||
type Target = [u8]; | ||
|
||
#[inline] | ||
fn deref(&self) -> &[u8] { | ||
// Safety: | ||
// `self.bytes` is valid per the construction in `slice_owned` | ||
// (which is the only constructor) | ||
unsafe { &*self.bytes } | ||
} | ||
} | ||
|
||
impl Borrow<[u8]> for OwnedSlice { | ||
#[inline] | ||
fn borrow(&self) -> &[u8] { | ||
self | ||
} | ||
} | ||
|
||
// Safety: `OwnedSlice` is conceptually `(&'self.1 [u8], Box<dyn Send + Sync>)`, which is `Send` | ||
unsafe impl Send for OwnedSlice {} | ||
|
||
// Safety: `OwnedSlice` is conceptually `(&'self.1 [u8], Box<dyn Send + Sync>)`, which is `Sync` | ||
unsafe impl Sync for OwnedSlice {} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::{ | ||
ops::Deref, | ||
sync::{ | ||
atomic::{self, AtomicBool}, | ||
Arc, | ||
}, | ||
}; | ||
|
||
use crate::{ | ||
owned_slice::{slice_owned, try_slice_owned, OwnedSlice}, | ||
OnDrop, | ||
}; | ||
|
||
#[test] | ||
fn smoke() { | ||
let slice = slice_owned(vec![1, 2, 3, 4, 5, 6], Vec::as_slice); | ||
|
||
assert_eq!(&*slice, [1, 2, 3, 4, 5, 6]); | ||
} | ||
|
||
#[test] | ||
fn static_storage() { | ||
let slice = slice_owned(Box::new(String::from("what")), |_| b"bytes boo"); | ||
|
||
assert_eq!(&*slice, b"bytes boo"); | ||
} | ||
|
||
#[test] | ||
fn slice_the_slice() { | ||
let slice = slice_owned(vec![1, 2, 3, 4, 5, 6], Vec::as_slice); | ||
let slice = slice_owned(slice, |s| &s[1..][..4]); | ||
let slice = slice_owned(slice, |s| s); | ||
let slice = slice_owned(slice, |s| &s[1..]); | ||
|
||
assert_eq!(&*slice, &[1, 2, 3, 4, 5, 6][1..][..4][1..]); | ||
} | ||
|
||
#[test] | ||
fn try_and_fail() { | ||
let res = try_slice_owned(vec![0], |v| v.get(12..).ok_or(())); | ||
|
||
assert!(res.is_err()); | ||
} | ||
|
||
#[test] | ||
fn boxed() { | ||
// It's important that we don't cause UB because of `Box`'es uniqueness | ||
|
||
let boxed: Box<[u8]> = vec![1, 1, 2, 3, 5, 8, 13, 21].into_boxed_slice(); | ||
let slice = slice_owned(boxed, Deref::deref); | ||
|
||
assert_eq!(&*slice, [1, 1, 2, 3, 5, 8, 13, 21]); | ||
} | ||
|
||
#[test] | ||
fn drop_drops() { | ||
let flag = Arc::new(AtomicBool::new(false)); | ||
let flag_prime = Arc::clone(&flag); | ||
let d = OnDrop(move || flag_prime.store(true, atomic::Ordering::Relaxed)); | ||
|
||
let slice = slice_owned(d, |_| &[]); | ||
|
||
assert_eq!(flag.load(atomic::Ordering::Relaxed), false); | ||
|
||
drop(slice); | ||
|
||
assert_eq!(flag.load(atomic::Ordering::Relaxed), true); | ||
} | ||
|
||
#[test] | ||
fn send_sync() { | ||
crate::sync::assert_send::<OwnedSlice>(); | ||
crate::sync::assert_sync::<OwnedSlice>(); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.