-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Document From::from
impls
#137330
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
base: master
Are you sure you want to change the base?
Document From::from
impls
#137330
Changes from all commits
013a044
050ad7a
10dfebd
c4a5190
e133fc7
4d83e90
44e4080
42fd425
5cddbcb
b20bd28
4cbfc84
199448b
894f270
124bcb2
858b821
59a2ea3
6891a03
84128a3
d03e4b8
750828d
464e54d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -204,6 +204,7 @@ impl Default for ByteString { | |||||||||
// | ||||||||||
// #[unstable(feature = "bstr", issue = "134915")] | ||||||||||
// impl From<Vec<u8>> for ByteString { | ||||||||||
// /// Make a `ByteString` with `Vec<u8>` as inner | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
// #[inline] | ||||||||||
// fn from(s: Vec<u8>) -> Self { | ||||||||||
// ByteString(s) | ||||||||||
|
@@ -222,6 +223,7 @@ impl From<ByteString> for Vec<u8> { | |||||||||
// | ||||||||||
// #[unstable(feature = "bstr", issue = "134915")] | ||||||||||
// impl<'a> From<&'a str> for ByteString { | ||||||||||
// /// Allocate a new `ByteString` with a copy of the bytes in the slice. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
// #[inline] | ||||||||||
// fn from(s: &'a str) -> Self { | ||||||||||
// ByteString(s.as_bytes().to_vec()) | ||||||||||
|
@@ -238,6 +240,7 @@ impl From<ByteString> for Vec<u8> { | |||||||||
|
||||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
impl<'a> From<&'a ByteStr> for ByteString { | ||||||||||
/// Allocates a `ByteString` containing the bytes of `ByteStr`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: &'a ByteStr) -> Self { | ||||||||||
ByteString(s.0.to_vec()) | ||||||||||
|
@@ -246,6 +249,7 @@ impl<'a> From<&'a ByteStr> for ByteString { | |||||||||
|
||||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
impl<'a> From<ByteString> for Cow<'a, ByteStr> { | ||||||||||
/// Wrap `ByteString` in `Cow::Owned`. | ||||||||||
#[inline] | ||||||||||
fn from(s: ByteString) -> Self { | ||||||||||
Cow::Owned(s) | ||||||||||
|
@@ -254,6 +258,7 @@ impl<'a> From<ByteString> for Cow<'a, ByteStr> { | |||||||||
|
||||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
impl<'a> From<&'a ByteString> for Cow<'a, ByteStr> { | ||||||||||
/// Wrap `ByteString` as byte str in `Cow::Borrowed`. | ||||||||||
TimTheBig marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: &'a ByteString) -> Self { | ||||||||||
Cow::Borrowed(s.as_bytestr()) | ||||||||||
|
@@ -599,6 +604,7 @@ impl Clone for Box<ByteStr> { | |||||||||
|
||||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> { | ||||||||||
/// Wrap `ByteStr` in `Cow::Borrowed`. | ||||||||||
#[inline] | ||||||||||
fn from(s: &'a ByteStr) -> Self { | ||||||||||
Cow::Borrowed(s) | ||||||||||
|
@@ -607,6 +613,7 @@ impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> { | |||||||||
|
||||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
impl From<Box<[u8]>> for Box<ByteStr> { | ||||||||||
/// Move the bytes from `Box<ByteStr>` to `Box<[u8]>`, this does not allocate new memory. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: Box<[u8]>) -> Box<ByteStr> { | ||||||||||
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. | ||||||||||
|
@@ -616,6 +623,7 @@ impl From<Box<[u8]>> for Box<ByteStr> { | |||||||||
|
||||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
impl From<Box<ByteStr>> for Box<[u8]> { | ||||||||||
/// Convert the inner bytes of `Box<[u8]>` to `ByteStr`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: Box<ByteStr>) -> Box<[u8]> { | ||||||||||
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. | ||||||||||
|
@@ -626,6 +634,7 @@ impl From<Box<ByteStr>> for Box<[u8]> { | |||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
#[cfg(not(no_rc))] | ||||||||||
impl From<Rc<[u8]>> for Rc<ByteStr> { | ||||||||||
/// Create an `Rc<[u8]>` from the inner bytes of `Rc<ByteStr>`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: Rc<[u8]>) -> Rc<ByteStr> { | ||||||||||
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. | ||||||||||
|
@@ -636,6 +645,7 @@ impl From<Rc<[u8]>> for Rc<ByteStr> { | |||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
#[cfg(not(no_rc))] | ||||||||||
impl From<Rc<ByteStr>> for Rc<[u8]> { | ||||||||||
/// Create a `Rc<ByteStr>` from the bytes of `Rc<[u8]>`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: Rc<ByteStr>) -> Rc<[u8]> { | ||||||||||
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. | ||||||||||
|
@@ -646,6 +656,7 @@ impl From<Rc<ByteStr>> for Rc<[u8]> { | |||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))] | ||||||||||
impl From<Arc<[u8]>> for Arc<ByteStr> { | ||||||||||
/// Create an `Arc<ByteStr>` from the bytes of `Arc<[u8]>`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: Arc<[u8]>) -> Arc<ByteStr> { | ||||||||||
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. | ||||||||||
|
@@ -656,6 +667,7 @@ impl From<Arc<[u8]>> for Arc<ByteStr> { | |||||||||
#[unstable(feature = "bstr", issue = "134915")] | ||||||||||
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))] | ||||||||||
impl From<Arc<ByteStr>> for Arc<[u8]> { | ||||||||||
/// Create a `Arc<[u8]>` from the inner bytes of `Arc<ByteStr>`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn from(s: Arc<ByteStr>) -> Arc<[u8]> { | ||||||||||
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`. | ||||||||||
|
@@ -675,6 +687,10 @@ impl_partial_eq_ord_cow!(&'a ByteStr, Cow<'a, [u8]>); | |||||||||
impl<'a> TryFrom<&'a ByteStr> for String { | ||||||||||
type Error = core::str::Utf8Error; | ||||||||||
|
||||||||||
/// Try to allocate a `ByteStr`s bytes as a UTF-8 `String`. | ||||||||||
/// | ||||||||||
/// # Errors | ||||||||||
/// If `ByteStr` is not valid UTF-8 | ||||||||||
Comment on lines
+690
to
+693
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
#[inline] | ||||||||||
fn try_from(s: &'a ByteStr) -> Result<Self, Self::Error> { | ||||||||||
Ok(core::str::from_utf8(&s.0)?.into()) | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -546,6 +546,7 @@ macro_rules! into_int_impl { | |||
$( | ||||
#[unstable(feature = "ascii_char", issue = "110998")] | ||||
impl From<AsciiChar> for $ty { | ||||
#[doc = concat!("Convert `AsciiChar` as `u8` into `", stringify!($ty), "`")] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(chr: AsciiChar) -> $ty { | ||||
chr as u8 as $ty | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -164,6 +164,7 @@ impl<T: ?Sized> Exclusive<T> { | |||
|
||||
#[unstable(feature = "exclusive_wrapper", issue = "98407")] | ||||
impl<T> From<T> for Exclusive<T> { | ||||
/// Creates a new `Exclusive` wrapping `T`. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(t: T) -> Self { | ||||
Self::new(t) | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,7 @@ macro_rules! from_transmute { | |||
}; | ||||
{ @impl $from:ty => $to:ty } => { | ||||
impl core::convert::From<$from> for $to { | ||||
#[doc = concat!("Transmute a `", stringify!($from), "` into a `", stringify!($to), "`")] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(value: $from) -> $to { | ||||
// Safety: transmuting between vectors is safe, but the caller of this macro | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -17,6 +17,7 @@ pub(super) struct Closure<'a, A, R> { | |||
struct Env; | ||||
|
||||
impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> { | ||||
/// Create a `Closure` from an `FnMut` eg.(function, `||` closure) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
fn from(f: &'a mut F) -> Self { | ||||
unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: *mut Env, arg: A) -> R { | ||||
unsafe { (*(env as *mut _ as *mut F))(arg) } | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -137,6 +137,7 @@ impl From<OwnedFd> for PidFd { | |||
} | ||||
|
||||
impl From<PidFd> for OwnedFd { | ||||
/// Get the inner of `PidFd` | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
fn from(pid_fd: PidFd) -> Self { | ||||
pid_fd.into_inner().into_inner().into_inner() | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -243,13 +243,15 @@ macro_rules! impl_owned_fd_traits { | |||
} | ||||
|
||||
impl From<net::$t> for OwnedFd { | ||||
#[doc = concat!("Create a `OwnedFd` from inner `", stringify!($t), "`")] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(socket: net::$t) -> OwnedFd { | ||||
socket.into_inner().into_socket().into_inner() | ||||
} | ||||
} | ||||
|
||||
impl From<OwnedFd> for net::$t { | ||||
#[doc = concat!("Create a `", stringify!($t), "` with inner `OwnedFd`")] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(owned_fd: OwnedFd) -> Self { | ||||
Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd))) | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -981,6 +981,7 @@ impl From<UnixDatagram> for OwnedFd { | |||
|
||||
#[stable(feature = "io_safety", since = "1.63.0")] | ||||
impl From<OwnedFd> for UnixDatagram { | ||||
/// Takes ownership of a [`OwnedFd`]'s socket file descriptor. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(owned: OwnedFd) -> Self { | ||||
unsafe { Self::from_raw_fd(owned.into_raw_fd()) } | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -694,6 +694,7 @@ impl From<UnixStream> for OwnedFd { | |||
|
||||
#[stable(feature = "io_safety", since = "1.63.0")] | ||||
impl From<OwnedFd> for UnixStream { | ||||
/// Takes ownership of a [`OwnedFd`]'s socket file descriptor. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(owned: OwnedFd) -> Self { | ||||
unsafe { Self::from_raw_fd(owned.into_raw_fd()) } | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -655,6 +655,7 @@ impl<T> AsHandle for crate::thread::JoinHandle<T> { | |||
|
||||
#[stable(feature = "io_safety", since = "1.63.0")] | ||||
impl<T> From<crate::thread::JoinHandle<T>> for OwnedHandle { | ||||
/// Get the inner handle of `JoinHandle`. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(join_handle: crate::thread::JoinHandle<T>) -> OwnedHandle { | ||||
join_handle.into_inner().into_handle().into_inner() | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -79,6 +79,7 @@ pub enum Error { | |||
|
||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||
impl From<usize> for Error { | ||||
/// Convert an error code to an `Error` | ||||
fn from(src: usize) -> Self { | ||||
match src { | ||||
0 => Self::NoError, | ||||
|
@@ -115,6 +116,7 @@ impl From<usize> for Error { | |||
|
||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||
impl From<i32> for Error { | ||||
/// Convert `i32` to an `Error`, if the conversion fails it `UnknownError` is returned | ||||
fn from(src: i32) -> Self { | ||||
let Ok(src) = core::convert::TryInto::<usize>::try_into(src) else { | ||||
return Self::UnknownError; | ||||
|
@@ -193,6 +195,7 @@ pub struct Connection(u32); | |||
|
||||
#[stable(feature = "rust1", since = "1.0.0")] | ||||
impl From<u32> for Connection { | ||||
/// Make a `Connection` with `u32` as inner | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
fn from(src: u32) -> Connection { | ||||
Connection(src) | ||||
} | ||||
|
@@ -263,6 +266,7 @@ impl Into<[u32; 4]> for ServerAddress { | |||
pub(crate) struct ThreadId(usize); | ||||
|
||||
impl From<usize> for ThreadId { | ||||
/// Make a `ThreadId` with `usize` as the id | ||||
TimTheBig marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
fn from(src: usize) -> ThreadId { | ||||
ThreadId(src) | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -47,6 +47,7 @@ pub enum Selected { | |||
} | ||||
|
||||
impl From<usize> for Selected { | ||||
/// Make a `Selected` from a `usize`, 0-2 match specific variants, the rest matches `Operation` | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
#[inline] | ||||
fn from(val: usize) -> Selected { | ||||
match val { | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clarifies that it is no copy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All From conversions between owned buffers like
String
,Vec
,ByteString
,OsString
, etc are no copyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a place were that is clearly stated?