Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Experiment with a safe adoption API #47

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Adopt::try_adopt
  • Loading branch information
lopopolo committed Jun 15, 2021
commit bfae7c6bdf8f81b20d7e4ed08becfff9927f6250
33 changes: 30 additions & 3 deletions src/adopt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt;
use core::ptr;

use crate::link::Link;
Expand All @@ -13,6 +14,21 @@ mod sealed {
impl<T> Sealed for Rc<T> {}
}

/// The error type for `try_adopt` methods.
///
/// See [`Adopt::try_adopt`] for more information.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AdoptError(());

#[cfg(feature = "std")]
impl std::error::Error for AdoptError {}

impl fmt::Display for AdoptError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Rc adoption failed because the Rc does not own a pointer to the adoptee")
}
}

/// Build a graph of linked [`Rc`] smart pointers to enable busting cycles on
/// drop.
///
Expand All @@ -34,12 +50,20 @@ mod sealed {
///
/// [`adopt_unchecked`]: Adopt::adopt_unchecked
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
pub unsafe trait Adopt: sealed::Sealed {
pub unsafe trait Adopt: sealed::Sealed + Sized {
/// The smart pointer's inner owned value.
type Inner;

/// TODO: document me!
fn adopt(this: &mut Self, other: &Self)
fn adopt(this: &mut Self, other: &Self) -> Self
where
Self::Inner: Trace,
{
Self::try_adopt(this, other).unwrap_or_else(|err| panic!("{}", err))
}

/// TODO: document me!
fn try_adopt(this: &mut Self, other: &Self) -> Result<Self, AdoptError>
where
Self::Inner: Trace;

Expand Down Expand Up @@ -92,7 +116,7 @@ unsafe impl<T> Adopt for Rc<T> {
type Inner = T;

/// TODO: document me!
fn adopt(this: &mut Self, other: &Self)
fn try_adopt(this: &mut Self, other: &Self) -> Result<Self, AdoptError>
where
Self::Inner: Trace,
{
Expand Down Expand Up @@ -125,6 +149,9 @@ unsafe impl<T> Adopt for Rc<T> {
unsafe {
Self::adopt_unchecked(this, &node);
}
Ok(node)
} else {
Err(AdoptError(()))
}
lopopolo marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ mod trace;
pub mod implementing_self_referential_data_structures;

pub use adopt::Adopt;
pub use adopt::AdoptError;
pub use rc::Rc;
pub use rc::Weak;
pub use trace::Trace;
Expand Down