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

Remove explicit negative Send and Sync impls #7

Merged
merged 1 commit into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
alloc_layout_extra,
box_into_raw_non_null,
dropck_eyepatch,
optin_builtin_traits,
specialization
)]
#![deny(warnings, intra_doc_link_resolution_failure, missing_docs)]
Expand Down Expand Up @@ -42,6 +41,8 @@
//! If you do not depend on these APIs, `cactusref` is a drop-in replacement for
//! [`std::rc`].
//!
//! Like [`std::rc`], [`Rc`] and [`Weak`] are `!`[`Send`] and `!`[`Sync`].
//!
//! ## Cycle Detection
//!
//! `Rc` implements [`Adoptable`] to log bookkeeping entries for strong
Expand Down
17 changes: 14 additions & 3 deletions src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ use crate::Weak;
/// that you have to call them as e.g., [`Rc::get_mut(&mut value)`](Rc::get_mut)
/// instead of `value.get_mut()`. This avoids conflicts with methods of the
/// inner type `T`.
///
/// Like [`std::rc::Rc`], `Rc` is `!`[`Send`] and `!`[`Sync`].
///
/// ```rust,compile_fail
/// # use cactusref::Rc;
/// fn requires_send<T: Send>(s: T) {}
/// requires_send(Rc::new(5));
/// ```
///
/// ```rust,compile_fail
/// # use cactusref::Rc;
/// fn requires_sync<T: Sync>(s: T) {}
/// requires_sync(Rc::new(5));
/// ```
pub struct Rc<T: ?Sized> {
pub(crate) ptr: NonNull<RcBox<T>>,
pub(crate) phantom: PhantomData<T>,
}

impl<T: ?Sized> !Send for Rc<T> {}
impl<T: ?Sized> !Sync for Rc<T> {}

impl<T> Rc<T> {
/// Constructs a new `Rc<T>`.
///
Expand Down
19 changes: 16 additions & 3 deletions src/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ use crate::Rc;
/// pointers from children back to their parents.
///
/// The typical way to obtain a `Weak` pointer is to call [`Rc::downgrade`].
///
/// Like [`std::rc::Weak`], `Weak` is `!`[`Send`] and `!`[`Sync`].
///
/// ```rust,compile_fail
/// # use cactusref::Rc;
/// fn requires_send<T: Send>(s: T) {}
/// let item = Rc::new(5);
/// requires_send(Rc::downgrade(&item));
/// ```
///
/// ```rust,compile_fail
/// # use cactusref::Rc;
/// fn requires_sync<T: Sync>(s: T) {}
/// let item = Rc::new(5);
/// requires_sync(Rc::downgrade(&item));
/// ```
pub struct Weak<T: ?Sized> {
// This is a `NonNull` to allow optimizing the size of this type in enums,
// but it is not necessarily a valid pointer.
Expand All @@ -33,9 +49,6 @@ pub struct Weak<T: ?Sized> {
pub(crate) ptr: NonNull<RcBox<T>>,
}

impl<T: ?Sized> !Send for Weak<T> {}
impl<T: ?Sized> !Sync for Weak<T> {}

impl<T> Weak<T> {
/// Constructs a new `Weak<T>`, without allocating any memory.
///
Expand Down