Skip to content

Add some AsRef implementations for smart pointers #139318

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,13 @@ impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
}
}

#[stable(feature = "smart_ptr_as_ref_str", since = "CURRENT_RUSTC_VERSION")]
impl<A: Allocator> AsRef<[u8]> for Box<str, A> {
fn as_ref(&self) -> &[u8] {
(&**self).as_ref()
}
}

#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
fn as_mut(&mut self) -> &mut T {
Expand Down
7 changes: 7 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3655,6 +3655,13 @@ impl<T: ?Sized, A: Allocator> AsRef<T> for Rc<T, A> {
}
}

#[stable(feature = "smart_ptr_as_ref_str", since = "CURRENT_RUSTC_VERSION")]
impl<A: Allocator> AsRef<[u8]> for Rc<str, A> {
fn as_ref(&self) -> &[u8] {
(&**self).as_ref()
}
}

#[stable(feature = "pin", since = "1.33.0")]
impl<T: ?Sized, A: Allocator> Unpin for Rc<T, A> {}

Expand Down
16 changes: 16 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,22 @@ impl AsRef<OsStr> for String {
}
}

#[stable(feature = "smart_ptr_as_ref_str", since = "CURRENT_RUSTC_VERSION")]
impl AsRef<OsStr> for Box<str> {
#[inline]
fn as_ref(&self) -> &OsStr {
(&**self).as_ref()
}
}

#[stable(feature = "smart_ptr_as_ref_str", since = "CURRENT_RUSTC_VERSION")]
impl AsRef<OsStr> for Rc<str> {
#[inline]
fn as_ref(&self) -> &OsStr {
(&**self).as_ref()
}
}

impl FromInner<Buf> for OsString {
#[inline]
fn from_inner(buf: Buf) -> OsString {
Expand Down
16 changes: 16 additions & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3358,6 +3358,22 @@ impl AsRef<Path> for PathBuf {
}
}

#[stable(feature = "smart_ptr_as_ref_str", since = "CURRENT_RUSTC_VERSION")]
impl AsRef<Path> for Box<str> {
#[inline]
fn as_ref(&self) -> &Path {
Path::new(self)
}
}

#[stable(feature = "smart_ptr_as_ref_str", since = "CURRENT_RUSTC_VERSION")]
impl AsRef<Path> for Rc<str> {
#[inline]
fn as_ref(&self) -> &Path {
Path::new(self)
}
}

#[stable(feature = "path_into_iter", since = "1.6.0")]
impl<'a> IntoIterator for &'a PathBuf {
type Item = &'a OsStr;
Expand Down
Loading