Skip to content

Commit

Permalink
Minimal fixes to use maybe-dangling crate to address noamtashma#3
Browse files Browse the repository at this point in the history
  • Loading branch information
luketpeterson committed Jan 15, 2025
1 parent 40f1213 commit cc27f75
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ name = "owning_ref"

[dependencies]
stable_deref_trait = "1.0.0"
maybe-dangling = "0.1.1"
64 changes: 43 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ fn main() {
```
*/

extern crate maybe_dangling;
use maybe_dangling::MaybeDangling;
extern crate stable_deref_trait;
pub use stable_deref_trait::{StableDeref as StableAddress, CloneStableDeref as CloneStableAddress};
use std::marker::PhantomData;
Expand All @@ -261,7 +263,7 @@ use std::marker::PhantomData;
///
/// For more details and examples, see the module and method docs.
pub struct OwningRef<'t, O, T: ?Sized> {
owner: O,
owner: MaybeDangling<O>,
reference: *const T,
marker: PhantomData<&'t T>,
}
Expand All @@ -276,7 +278,7 @@ pub struct OwningRef<'t, O, T: ?Sized> {
///
/// For more details and examples, see the module and method docs.
pub struct OwningRefMut<'t, O, T: ?Sized> {
owner: O,
owner: MaybeDangling<O>,
reference: *mut T,
marker: PhantomData<&'t T>,
}
Expand Down Expand Up @@ -319,9 +321,10 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {
where O: StableAddress,
O: Deref<Target = T>,
{
let owner = MaybeDangling::new(o);
OwningRef {
reference: &*o,
owner: o,
reference: &**owner,
owner,
marker: PhantomData,
}
}
Expand All @@ -334,9 +337,10 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {
pub unsafe fn new_assert_stable_address(o: O) -> Self
where O: Deref<Target = T>,
{
let owner = MaybeDangling::new(o);
OwningRef {
reference: &*o,
owner: o,
reference: &**owner,
owner,
marker: PhantomData,
}
}
Expand Down Expand Up @@ -459,7 +463,6 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {
// println!("{:?}", ow_ref);
// drop(box_i);
// }


/// Old version of `try_map_with_owner`, now recognized as unsafe.
#[deprecated(since = "0.5.0", note = "unsafe function: please use try_map_with_owner instead")]
Expand Down Expand Up @@ -520,7 +523,7 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {
{
OwningRef {
reference: self.reference,
owner: f(self.owner),
owner: MaybeDangling::new(f(MaybeDangling::into_inner(self.owner))),
marker: PhantomData,
}
}
Expand All @@ -533,7 +536,7 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {
pub fn map_owner_box(self) -> OwningRef<'t, Box<O>, T> {
OwningRef {
reference: self.reference,
owner: Box::new(self.owner),
owner: MaybeDangling::new(Box::new(MaybeDangling::into_inner(self.owner))),
marker: PhantomData,
}
}
Expand Down Expand Up @@ -575,7 +578,7 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {
{
OwningRef {
reference: self.reference,
owner: self.owner.into_erased(),
owner: MaybeDangling::new(MaybeDangling::into_inner(self.owner).into_erased()),
marker: PhantomData,
}
}
Expand All @@ -589,7 +592,7 @@ impl<'t, O, T: ?Sized> OwningRef<'t, O, T> {

/// Discards the reference and retrieves the owner.
pub fn into_owner(self) -> O {
self.owner
MaybeDangling::into_inner(self.owner)
}
}

Expand All @@ -607,13 +610,14 @@ impl<'t, O, T: ?Sized> OwningRefMut<'t, O, T> {
/// assert_eq!(*owning_ref_mut, 42);
/// }
/// ```
pub fn new(mut o: O) -> Self
pub fn new(o: O) -> Self
where O: StableAddress,
O: DerefMut<Target = T>,
{
let mut owner = MaybeDangling::new(o);
OwningRefMut {
reference: &mut *o,
owner: o,
reference: &mut **owner,
owner,
marker: PhantomData,
}
}
Expand All @@ -623,12 +627,13 @@ impl<'t, O, T: ?Sized> OwningRefMut<'t, O, T> {
///
/// This is useful for cases where coherence rules prevents implementing the trait
/// without adding a dependency to this crate in a third-party library.
pub unsafe fn new_assert_stable_address(mut o: O) -> Self
pub unsafe fn new_assert_stable_address(o: O) -> Self
where O: DerefMut<Target = T>,
{
let mut owner = MaybeDangling::new(o);
OwningRefMut {
reference: &mut *o,
owner: o,
reference: &mut **owner,
owner,
marker: PhantomData,
}
}
Expand Down Expand Up @@ -779,7 +784,7 @@ impl<'t, O, T: ?Sized> OwningRefMut<'t, O, T> {
{
OwningRefMut {
reference: self.reference,
owner: f(self.owner),
owner: MaybeDangling::new(f(MaybeDangling::into_inner(self.owner))),
marker: PhantomData,
}
}
Expand All @@ -792,7 +797,7 @@ impl<'t, O, T: ?Sized> OwningRefMut<'t, O, T> {
pub fn map_owner_box(self) -> OwningRefMut<'t, Box<O>, T> {
OwningRefMut {
reference: self.reference,
owner: Box::new(self.owner),
owner: MaybeDangling::new(Box::new(MaybeDangling::into_inner(self.owner))),
marker: PhantomData,
}
}
Expand Down Expand Up @@ -834,7 +839,7 @@ impl<'t, O, T: ?Sized> OwningRefMut<'t, O, T> {
{
OwningRefMut {
reference: self.reference,
owner: self.owner.into_erased(),
owner: MaybeDangling::new(MaybeDangling::into_inner(self.owner).into_erased()),
marker: PhantomData,
}
}
Expand All @@ -853,7 +858,7 @@ impl<'t, O, T: ?Sized> OwningRefMut<'t, O, T> {

/// Discards the reference and retrieves the owner.
pub fn into_owner(self) -> O {
self.owner
MaybeDangling::into_inner(self.owner)
}
}

Expand Down Expand Up @@ -2080,4 +2085,21 @@ mod tests {
assert_eq!(*d, 45);
}
}

/// This test threw an error under miri. See https://github.com/noamtashma/owning-ref-rs/issues/3
#[test]
fn noalias_test() {
use super::OwningRef;
use std::cell::Cell;

fn helper(owning_ref: OwningRef<Box<Cell<u8>>, Cell<u8>>) -> u8 {
owning_ref.set(20);
owning_ref.as_owner().get() // should return 20
}

let val: Box<Cell<u8>> = Box::new(Cell::new(25));
let owning_ref = OwningRef::new(val);
let res = helper(owning_ref);
assert_eq!(res, 20);
}
}

0 comments on commit cc27f75

Please sign in to comment.