From 64839ee00ab4076d797901ddea55f2613c5b75b5 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 8 Oct 2020 23:51:56 +0200 Subject: [PATCH 1/6] Add Pin::new_static. --- library/core/src/pin.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 9f0284d5d95..cc93f0fa5fc 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -781,6 +781,19 @@ impl<'a, T: ?Sized> Pin<&'a mut T> { } } +impl Pin<&'static T> { + /// Get a pinned reference from a static reference. + /// + /// This is safe, because the `'static` lifetime guarantees the data will + /// never be moved. + #[unstable(feature = "pin_static_ref", issue = "none")] + pub fn new_static(r: &'static T) -> Pin<&'static T> { + // SAFETY: The 'static lifetime guarantees the data will not be + // moved/invalidated until it gets dropped (which is never). + unsafe { Pin::new_unchecked(r) } + } +} + #[stable(feature = "pin", since = "1.33.0")] impl Deref for Pin

{ type Target = P::Target; From 390883e888c580d054ab4a2584c851aba50865e9 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 9 Oct 2020 00:06:39 +0200 Subject: [PATCH 2/6] Make Pin::new_static const. --- library/core/src/pin.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index cc93f0fa5fc..3f058124d2b 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -787,7 +787,8 @@ impl Pin<&'static T> { /// This is safe, because the `'static` lifetime guarantees the data will /// never be moved. #[unstable(feature = "pin_static_ref", issue = "none")] - pub fn new_static(r: &'static T) -> Pin<&'static T> { + #[rustc_const_unstable(feature = "const_pin", issue = "76654")] + pub const fn new_static(r: &'static T) -> Pin<&'static T> { // SAFETY: The 'static lifetime guarantees the data will not be // moved/invalidated until it gets dropped (which is never). unsafe { Pin::new_unchecked(r) } From 104c0f0194177442ff16cf14907a96d2f8d200dd Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 12 Oct 2020 20:00:44 +0200 Subject: [PATCH 3/6] Rename Pin::new_static to Pin::static_ref. --- library/core/src/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 3f058124d2b..9e2d64a866f 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -788,7 +788,7 @@ impl Pin<&'static T> { /// never be moved. #[unstable(feature = "pin_static_ref", issue = "none")] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] - pub const fn new_static(r: &'static T) -> Pin<&'static T> { + pub const fn static_ref(r: &'static T) -> Pin<&'static T> { // SAFETY: The 'static lifetime guarantees the data will not be // moved/invalidated until it gets dropped (which is never). unsafe { Pin::new_unchecked(r) } From 2c71f682d74d13ae6673dc701a9bb3a0562f57c0 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 12 Oct 2020 20:00:56 +0200 Subject: [PATCH 4/6] Add Pin::static_mut. --- library/core/src/pin.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 9e2d64a866f..b27167a7e7c 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -795,6 +795,20 @@ impl Pin<&'static T> { } } +impl Pin<&'static T> { + /// Get a pinned mutable reference from a static mutable reference. + /// + /// This is safe, because the `'static` lifetime guarantees the data will + /// never be moved. + #[unstable(feature = "pin_static_ref", issue = "none")] + #[rustc_const_unstable(feature = "const_pin", issue = "76654")] + pub const fn static_mut(r: &'static mut T) -> Pin<&'static mut T> { + // SAFETY: The 'static lifetime guarantees the data will not be + // moved/invalidated until it gets dropped (which is never). + unsafe { Pin::new_unchecked(r) } + } +} + #[stable(feature = "pin", since = "1.33.0")] impl Deref for Pin

{ type Target = P::Target; From f83446b836900ce9afbaa0816a5b4feda654b51e Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 13 Oct 2020 13:13:09 +0200 Subject: [PATCH 5/6] Reword safety guarantee of Pin::static_{ref,mut}. Co-authored-by: Peter Todd --- library/core/src/pin.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index b27167a7e7c..633e96eb7d8 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -784,12 +784,12 @@ impl<'a, T: ?Sized> Pin<&'a mut T> { impl Pin<&'static T> { /// Get a pinned reference from a static reference. /// - /// This is safe, because the `'static` lifetime guarantees the data will - /// never be moved. + /// This is safe, because `T` is borrowed for the `'static` lifetime, which + /// never ends. #[unstable(feature = "pin_static_ref", issue = "none")] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] pub const fn static_ref(r: &'static T) -> Pin<&'static T> { - // SAFETY: The 'static lifetime guarantees the data will not be + // SAFETY: The 'static borrow guarantees the data will not be // moved/invalidated until it gets dropped (which is never). unsafe { Pin::new_unchecked(r) } } @@ -798,12 +798,12 @@ impl Pin<&'static T> { impl Pin<&'static T> { /// Get a pinned mutable reference from a static mutable reference. /// - /// This is safe, because the `'static` lifetime guarantees the data will - /// never be moved. + /// This is safe, because `T` is borrowed for the `'static` lifetime, which + /// never ends. #[unstable(feature = "pin_static_ref", issue = "none")] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] pub const fn static_mut(r: &'static mut T) -> Pin<&'static mut T> { - // SAFETY: The 'static lifetime guarantees the data will not be + // SAFETY: The 'static borrow guarantees the data will not be // moved/invalidated until it gets dropped (which is never). unsafe { Pin::new_unchecked(r) } } From df95dcebf5f98cefdc60c9b9d818fb285ac07d5b Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 15 Oct 2020 21:45:09 +0200 Subject: [PATCH 6/6] Add missing `mut`. Co-authored-by: David Tolnay --- library/core/src/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 633e96eb7d8..b73cd046e5a 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -795,7 +795,7 @@ impl Pin<&'static T> { } } -impl Pin<&'static T> { +impl Pin<&'static mut T> { /// Get a pinned mutable reference from a static mutable reference. /// /// This is safe, because `T` is borrowed for the `'static` lifetime, which