Skip to content

Commit e3c6e46

Browse files
committed
Make some methods of Pin<&mut T> unstable const
Make the following methods unstable const under the `const_pin` feature: - `into_ref` - `get_mut` - `get_unchecked_mut`
1 parent 8f27e3c commit e3c6e46

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

library/core/src/pin.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,9 @@ impl<'a, T: ?Sized> Pin<&'a T> {
708708
impl<'a, T: ?Sized> Pin<&'a mut T> {
709709
/// Converts this `Pin<&mut T>` into a `Pin<&T>` with the same lifetime.
710710
#[inline(always)]
711+
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
711712
#[stable(feature = "pin", since = "1.33.0")]
712-
pub fn into_ref(self) -> Pin<&'a T> {
713+
pub const fn into_ref(self) -> Pin<&'a T> {
713714
Pin { pointer: self.pointer }
714715
}
715716

@@ -722,9 +723,10 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
722723
/// that lives for as long as the borrow of the `Pin`, not the lifetime of
723724
/// the `Pin` itself. This method allows turning the `Pin` into a reference
724725
/// with the same lifetime as the original `Pin`.
725-
#[stable(feature = "pin", since = "1.33.0")]
726726
#[inline(always)]
727-
pub fn get_mut(self) -> &'a mut T
727+
#[stable(feature = "pin", since = "1.33.0")]
728+
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
729+
pub const fn get_mut(self) -> &'a mut T
728730
where
729731
T: Unpin,
730732
{
@@ -741,9 +743,10 @@ impl<'a, T: ?Sized> Pin<&'a mut T> {
741743
///
742744
/// If the underlying data is `Unpin`, `Pin::get_mut` should be used
743745
/// instead.
744-
#[stable(feature = "pin", since = "1.33.0")]
745746
#[inline(always)]
746-
pub unsafe fn get_unchecked_mut(self) -> &'a mut T {
747+
#[stable(feature = "pin", since = "1.33.0")]
748+
#[rustc_const_unstable(feature = "const_pin", issue = "76654")]
749+
pub const unsafe fn get_unchecked_mut(self) -> &'a mut T {
747750
self.pointer
748751
}
749752

library/core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#![feature(iter_order_by)]
4040
#![feature(cmp_min_max_by)]
4141
#![feature(iter_map_while)]
42+
#![feature(const_mut_refs)]
4243
#![feature(const_pin)]
4344
#![feature(const_slice_from_raw_parts)]
4445
#![feature(const_raw_ptr_deref)]

library/core/tests/pin.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,15 @@ fn pin_const() {
1717
assert_eq!(INNER_UNCHECKED, POINTER);
1818

1919
const REF: &'static usize = PINNED.get_ref();
20-
assert_eq!(REF, POINTER)
20+
assert_eq!(REF, POINTER);
21+
22+
// Note: `pin_mut_const` tests that the methods of `Pin<&mut T>` are usable in a const context.
23+
// A const fn is used because `&mut` is not (yet) usable in constants.
24+
const fn pin_mut_const() {
25+
let _ = Pin::new(&mut 2).into_ref();
26+
let _ = Pin::new(&mut 2).get_mut();
27+
let _ = unsafe { Pin::new(&mut 2).get_unchecked_mut() };
28+
}
29+
30+
pin_mut_const();
2131
}

0 commit comments

Comments
 (0)