From 1eed8ed6cd73df448b1795058517fa72dd5c72ef Mon Sep 17 00:00:00 2001 From: CAD97 Date: Sun, 24 May 2020 11:39:30 -0400 Subject: [PATCH] Add [A]RcBorrow -> Weak conversion without strong clone --- Cargo.lock | 59 ++++++++++++++---------------- crates/rc-borrow/build.rs | 8 ----- crates/rc-borrow/src/lib.rs | 71 ++++++++++++++++++++----------------- 3 files changed, 66 insertions(+), 72 deletions(-) delete mode 100644 crates/rc-borrow/build.rs diff --git a/Cargo.lock b/Cargo.lock index 1e2df26..494b00e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,7 +14,7 @@ checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" [[package]] name = "erasable" -version = "1.2.0" +version = "1.2.1" dependencies = [ "autocfg", "either", @@ -23,9 +23,9 @@ dependencies = [ [[package]] name = "erasable" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c18404d99ecdee16598948437ef1a18b94c74f4d3a374cd41f642626c2c57cd8" +checksum = "5f11890ce181d47a64e5d1eb4b6caba0e7bae911a356723740d058a5d0340b7d" dependencies = [ "autocfg", "scopeguard", @@ -33,9 +33,9 @@ dependencies = [ [[package]] name = "paste" -version = "0.1.10" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab4fb1930692d1b6a9cfabdde3d06ea0a7d186518e2f4d67660d8970e2fa647a" +checksum = "678f27e19361472a23717f11d229a7522ef64605baf0715c896a94b8b6b13a06" dependencies = [ "paste-impl", "proc-macro-hack", @@ -43,9 +43,9 @@ dependencies = [ [[package]] name = "paste-impl" -version = "0.1.10" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a62486e111e571b1e93b710b61e8f493c0013be39629b714cb166bdb06aa5a8a" +checksum = "149089128a45d8e377677b08873b4bad2a56618f80e4f28a83862ba250994a30" dependencies = [ "proc-macro-hack", "proc-macro2", @@ -55,56 +55,51 @@ dependencies = [ [[package]] name = "proc-macro-hack" -version = "0.5.11" +version = "0.5.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] +checksum = "7e0456befd48169b9f13ef0f0ad46d492cf9d2dbb918bcf38e01eed4ce3ec5e4" [[package]] name = "proc-macro2" -version = "1.0.8" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" +checksum = "1502d12e458c49a4c9cbff560d0fe0060c252bc29799ed94ca2ed4bb665a0101" dependencies = [ "unicode-xid", ] [[package]] name = "ptr-union" -version = "2.0.0" +version = "2.0.1" dependencies = [ "autocfg", - "erasable 1.2.0", + "erasable 1.2.1", "paste", ] [[package]] name = "quote" -version = "1.0.2" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +checksum = "54a21852a652ad6f610c9510194f398ff6f8692e334fd1145fed931f7fbe44ea" dependencies = [ "proc-macro2", ] [[package]] name = "rc-borrow" -version = "1.2.0" +version = "1.2.1" dependencies = [ "autocfg", - "erasable 1.2.0", + "erasable 1.2.1", ] [[package]] name = "rc-box" -version = "1.1.0" +version = "1.1.1" dependencies = [ - "erasable 1.2.0", - "slice-dst 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "erasable 1.2.1", + "slice-dst 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -115,27 +110,27 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "slice-dst" -version = "1.4.0" +version = "1.4.1" dependencies = [ "autocfg", - "erasable 1.2.0", + "erasable 1.2.1", ] [[package]] name = "slice-dst" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf8103682684791d9a6db538a391935eccd438b723d08017418ae0d07e91c1d7" +checksum = "9fad5402bd04a3a3209ee22a021be871f54519bf427f50d4d2c9e19ab11f7d36" dependencies = [ "autocfg", - "erasable 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "erasable 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syn" -version = "1.0.15" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a0294dc449adc58bb6592fff1a23d3e5e6e235afc6a0ffca2657d19e7bbffe5" +checksum = "f87bc5b2815ebb664de0392fdf1b95b6d10e160f86d9f64ff65e5679841ca06a" dependencies = [ "proc-macro2", "quote", diff --git a/crates/rc-borrow/build.rs b/crates/rc-borrow/build.rs deleted file mode 100644 index 7dd4244..0000000 --- a/crates/rc-borrow/build.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn main() { - let cfg = autocfg::new(); - cfg.emit_expression_cfg("std::sync::Arc::as_raw", "has_Arc__into_raw"); - cfg.emit_expression_cfg("std::sync::Arc::clone_raw", "has_Arc__clone_raw"); - cfg.emit_expression_cfg("std::rc::Rc::as_raw", "has_Rc__into_raw"); - cfg.emit_expression_cfg("std::rc::Rc::clone_raw", "has_Rc__clone_raw"); - autocfg::rerun_path("build.rs"); -} diff --git a/crates/rc-borrow/src/lib.rs b/crates/rc-borrow/src/lib.rs index a308ae6..623a580 100644 --- a/crates/rc-borrow/src/lib.rs +++ b/crates/rc-borrow/src/lib.rs @@ -35,7 +35,10 @@ use std::{ panic::{RefUnwindSafe, UnwindSafe}, }; use { - alloc::{rc::Rc, sync::Arc}, + alloc::{ + rc::{self, Rc}, + sync::{self, Arc}, + }, core::{ borrow::Borrow, cmp::Ordering, @@ -55,59 +58,58 @@ use { /// See https://internals.rust-lang.org/t/_/11463/11 for why these are important. /// By using a trait here, we can more easily switch when these functions are available. trait RawRc { + type Weak; + //noinspection RsSelfConvention fn as_raw(this: &Self) -> *const T; + /// # Safety /// /// This pointer must have come from [`RawRc::as_raw`] or `into_raw`. unsafe fn clone_raw(this: *const T) -> Self; + + unsafe fn downgrade_raw(this: *const T) -> Self::Weak; } impl RawRc for Arc { - #[rustfmt::skip] + type Weak = sync::Weak; + #[inline(always)] fn as_raw(this: &Self) -> *const T { - #[cfg(not(has_Arc__as_raw))] { - Arc::into_raw(unsafe { ptr::read(this) }) - } - #[cfg(has_Arc__as_raw)] { - Arc::as_raw(this) - } + // Arc::as_ptr(this) + Arc::into_raw(unsafe { ptr::read(this) }) } - #[rustfmt::skip] #[inline(always)] unsafe fn clone_raw(this: *const T) -> Self { - #[cfg(not(has_Arc__clone_raw))] { - Arc::clone(&ManuallyDrop::new(Arc::from_raw(this))) - } - #[cfg(has_Arc__clone_raw)] { - Arc::clone_raw(this) - } + Arc::clone(&ManuallyDrop::new(Arc::from_raw(this))) + } + + #[inline(always)] + unsafe fn downgrade_raw(this: *const T) -> sync::Weak { + let this = ManuallyDrop::new(Arc::from_raw(this)); + Arc::downgrade(&this) } } impl RawRc for Rc { - #[rustfmt::skip] + type Weak = rc::Weak; + #[inline(always)] fn as_raw(this: &Self) -> *const T { - #[cfg(not(has_Rc__as_raw))] { - Rc::into_raw(unsafe { ptr::read(this) }) - } - #[cfg(has_Rc__as_raw)] { - Rc::as_raw(this) - } + // Rc::as_ptr(this) + Rc::into_raw(unsafe { ptr::read(this) }) } - #[rustfmt::skip] #[inline(always)] unsafe fn clone_raw(this: *const T) -> Self { - #[cfg(not(has_Rc__clone_raw))] { - Rc::clone(&ManuallyDrop::new(Rc::from_raw(this))) - } - #[cfg(has_Rc__clone_raw)] { - Rc::clone_raw(this) - } + Rc::clone(&ManuallyDrop::new(Rc::from_raw(this))) + } + + #[inline(always)] + unsafe fn downgrade_raw(this: *const T) -> rc::Weak { + let this = ManuallyDrop::new(Rc::from_raw(this)); + Rc::downgrade(&this) } } @@ -120,7 +122,7 @@ macro_rules! doc_comment { } macro_rules! rc_borrow { - ($($(#[$m:meta])* $vis:vis struct $RcBorrow:ident = &$Rc:ident;)*) => {$( + ($($(#[$m:meta])* $vis:vis struct $RcBorrow:ident = &$rc:ident::$Rc:ident;)*) => {$( $(#[$m])* $vis struct $RcBorrow<'a, T: ?Sized> { raw: ptr::NonNull, @@ -147,6 +149,11 @@ macro_rules! rc_borrow { unsafe { <$Rc as RawRc>::clone_raw(this.raw.as_ptr()) } } + /// Convert this borrowed pointer into a weak pointer. + $vis fn to_weak(this: Self) -> $rc::Weak { + unsafe { <$Rc as RawRc>::downgrade_raw(this.raw.as_ptr()) } + } + /// Convert this borrowed pointer into a standard reference. /// /// This gives you a long-lived reference, @@ -377,10 +384,10 @@ rc_borrow! { /// /// This type is guaranteed to have the same repr as `&T`. #[repr(transparent)] - pub struct ArcBorrow = &Arc; + pub struct ArcBorrow = &sync::Arc; /// Borrowed version of [`Rc`]. /// /// This type is guaranteed to have the same repr as `&T`. #[repr(transparent)] - pub struct RcBorrow = &Rc; + pub struct RcBorrow = &rc::Rc; }