Skip to content

Commit 4e26f96

Browse files
refactor tests and helpers as requested
1 parent 5282939 commit 4e26f96

File tree

5 files changed

+51
-99
lines changed

5 files changed

+51
-99
lines changed

library/core/src/cell.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ impl<T> RefCell<T> {
910910
#[rustc_confusables("swap")]
911911
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
912912
pub const fn replace(&self, t: T) -> T {
913-
mem::replace(self.borrow_mut().as_mut(), t)
913+
mem::replace(&mut self.borrow_mut(), t)
914914
}
915915

916916
/// Replaces the wrapped value with a new one computed from `f`, returning
@@ -962,7 +962,7 @@ impl<T> RefCell<T> {
962962
#[stable(feature = "refcell_swap", since = "1.24.0")]
963963
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
964964
pub const fn swap(&self, other: &Self) {
965-
mem::swap(self.borrow_mut().as_mut(), other.borrow_mut().as_mut())
965+
mem::swap(&mut *self.borrow_mut(), &mut *other.borrow_mut())
966966
}
967967
}
968968

@@ -1452,6 +1452,8 @@ impl<'b> BorrowRef<'b> {
14521452
Some(BorrowRef { borrow })
14531453
}
14541454
}
1455+
1456+
/// `Clone` is not a `const_trait`, so work around that by making our own method
14551457
#[inline]
14561458
#[rustc_const_unstable(feature = "const_ref_cell", issue = "137844")]
14571459
const fn clone(&self) -> Self {
@@ -1671,12 +1673,6 @@ impl<T: ?Sized + fmt::Display> fmt::Display for Ref<'_, T> {
16711673
}
16721674

16731675
impl<'b, T: ?Sized> RefMut<'b, T> {
1674-
#[inline]
1675-
const fn as_mut(&mut self) -> &mut T {
1676-
// SAFETY: the value is accessible as long as we hold our borrow.
1677-
unsafe { self.value.as_mut() }
1678-
}
1679-
16801676
/// Makes a new `RefMut` for a component of the borrowed data, e.g., an enum
16811677
/// variant.
16821678
///
@@ -1897,7 +1893,8 @@ pub struct RefMut<'b, T: ?Sized + 'b> {
18971893
}
18981894

18991895
#[stable(feature = "rust1", since = "1.0.0")]
1900-
impl<T: ?Sized> Deref for RefMut<'_, T> {
1896+
#[rustc_const_unstable(feature = "const_deref", issue = "88955")]
1897+
impl<T: ?Sized> const Deref for RefMut<'_, T> {
19011898
type Target = T;
19021899

19031900
#[inline]
@@ -1908,10 +1905,12 @@ impl<T: ?Sized> Deref for RefMut<'_, T> {
19081905
}
19091906

19101907
#[stable(feature = "rust1", since = "1.0.0")]
1911-
impl<T: ?Sized> DerefMut for RefMut<'_, T> {
1908+
#[rustc_const_unstable(feature = "const_deref", issue = "88955")]
1909+
impl<T: ?Sized> const DerefMut for RefMut<'_, T> {
19121910
#[inline]
19131911
fn deref_mut(&mut self) -> &mut T {
1914-
self.as_mut()
1912+
// SAFETY: the value is accessible as long as we hold our borrow.
1913+
unsafe { self.value.as_mut() }
19151914
}
19161915
}
19171916

library/coretests/tests/cell.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::cell::*;
2+
use core::mem::forget;
23

34
#[test]
45
fn smoketest_unsafe_cell() {
@@ -477,3 +478,42 @@ fn const_cells() {
477478
const _: i32 = CELL.into_inner();
478479
*/
479480
}
481+
482+
#[allow(dead_code)]
483+
fn const_refcell() {
484+
struct Dummy;
485+
impl Drop for Dummy {
486+
fn drop(&mut self) {
487+
panic!("should never be called");
488+
}
489+
}
490+
// Check that `replace` is usable at compile-time
491+
const REPLACE_TEST: u32 = {
492+
let a = RefCell::new(0);
493+
assert!(a.replace(10) == 0);
494+
let a = a.into_inner();
495+
assert!(a == 10);
496+
a
497+
};
498+
const REPLACE_DUMMY_TEST: RefCell<Dummy> = {
499+
let a = RefCell::new(Dummy);
500+
forget(a.replace(Dummy));
501+
a
502+
};
503+
504+
// Check that `swap` is usable at compile-time
505+
const SWAP_TEST: (u32, u32) = {
506+
let (a, b) = (RefCell::new(31), RefCell::new(41));
507+
a.swap(&b);
508+
let (a, b) = (a.into_inner(), b.into_inner());
509+
assert!(a == 41);
510+
assert!(b == 31);
511+
(a, b)
512+
};
513+
const SWAP_DUMMY_TEST: [RefCell<Dummy>; 2] = {
514+
let a = RefCell::new(Dummy);
515+
let b = RefCell::new(Dummy);
516+
a.swap(&b);
517+
[a, b]
518+
};
519+
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![feature(char_max_len)]
1717
#![feature(clone_to_uninit)]
1818
#![feature(const_eval_select)]
19+
#![feature(const_ref_cell)]
1920
#![feature(const_swap_nonoverlapping)]
2021
#![feature(const_trait_impl)]
2122
#![feature(core_intrinsics)]

tests/ui/consts/const-refcell-destruct.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/ui/consts/const-refcell.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)