Skip to content

Implement PointerLike for isize, NonNull, Cell, UnsafeCell, and SyncUnsafeCell. #134642

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

Merged
merged 1 commit into from
Dec 23, 2024
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
11 changes: 10 additions & 1 deletion library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@

use crate::cmp::Ordering;
use crate::fmt::{self, Debug, Display};
use crate::marker::{PhantomData, Unsize};
use crate::marker::{PhantomData, PointerLike, Unsize};
use crate::mem;
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
use crate::pin::PinCoerceUnsized;
Expand Down Expand Up @@ -677,6 +677,9 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<Cell<U>> for Cell<T> {}
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<Cell<U>> for Cell<T> {}

#[unstable(feature = "pointer_like_trait", issue = "none")]
impl<T: PointerLike> PointerLike for Cell<T> {}

impl<T> Cell<[T]> {
/// Returns a `&[Cell<T>]` from a `&Cell<[T]>`
///
Expand Down Expand Up @@ -2258,6 +2261,9 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T> {}

#[unstable(feature = "pointer_like_trait", issue = "none")]
impl<T: PointerLike> PointerLike for UnsafeCell<T> {}

/// [`UnsafeCell`], but [`Sync`].
///
/// This is just an `UnsafeCell`, except it implements `Sync`
Expand Down Expand Up @@ -2364,6 +2370,9 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<SyncUnsafeCell<U>> for SyncUnsafeCell
//#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<SyncUnsafeCell<U>> for SyncUnsafeCell<T> {}

#[unstable(feature = "pointer_like_trait", issue = "none")]
impl<T: PointerLike> PointerLike for SyncUnsafeCell<T> {}

#[allow(unused)]
fn assert_coerce_unsized(
a: UnsafeCell<&i32>,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ pub trait PointerLike {}
marker_impls! {
#[unstable(feature = "pointer_like_trait", issue = "none")]
PointerLike for
isize,
usize,
{T} &T,
{T} &mut T,
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,10 @@ impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: U
#[stable(feature = "pin", since = "1.33.0")]
unsafe impl<T: ?Sized> PinCoerceUnsized for NonNull<T> {}

#[unstable(feature = "pointer_like_trait", issue = "none")]
#[cfg(not(bootstrap))]
impl<T> core::marker::PointerLike for NonNull<T> {}

#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: ?Sized> fmt::Debug for NonNull<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
34 changes: 34 additions & 0 deletions tests/ui/dyn-star/cell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This test with Cell also indirectly exercises UnsafeCell in dyn*.
//
//@ run-pass

#![feature(dyn_star)]
#![allow(incomplete_features)]

use std::cell::Cell;

trait Rw<T> {
fn read(&self) -> T;
fn write(&self, v: T);
}

impl<T: Copy> Rw<T> for Cell<T> {
fn read(&self) -> T {
self.get()
}
fn write(&self, v: T) {
self.set(v)
}
}

fn make_dyn_star() -> dyn* Rw<usize> {
Cell::new(42usize) as dyn* Rw<usize>
}

fn main() {
let x = make_dyn_star();

assert_eq!(x.read(), 42);
x.write(24);
assert_eq!(x.read(), 24);
}
2 changes: 1 addition & 1 deletion tests/ui/dyn-star/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::Debug;
trait Foo {}

fn make_dyn_star() {
let i = 42;
let i = 42usize;
let dyn_i: dyn* Foo = i; //~ ERROR trait bound `usize: Foo` is not satisfied
}

Expand Down
4 changes: 3 additions & 1 deletion tests/ui/dyn-star/float-as-dyn-star.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ LL | f32::from_bits(0x1) as f64
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `f64` needs to be a pointer-like type
|
= help: the trait `PointerLike` is not implemented for `f64`
= help: the trait `PointerLike` is implemented for `usize`
= help: the following other types implement trait `PointerLike`:
isize
usize

error: aborting due to 1 previous error; 1 warning emitted

Expand Down
Loading