Skip to content
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

Document From::from impls #137330

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
36 changes: 36 additions & 0 deletions library/alloc/src/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ impl Default for ByteString {
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a, const N: usize> From<&'a [u8; N]> for ByteString {
// /// Make a `ByteString` from a byte array ref.
// ///
// /// ## Cost
// /// Allocates a new `Vec`
// #[inline]
// fn from(s: &'a [u8; N]) -> Self {
// ByteString(s.as_slice().to_vec())
Expand All @@ -188,6 +192,10 @@ impl Default for ByteString {
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<const N: usize> From<[u8; N]> for ByteString {
// /// Make a `ByteString` from a byte array.
// ///
// /// ## Cost
// /// Allocates a new `Vec`
// #[inline]
// fn from(s: [u8; N]) -> Self {
// ByteString(s.as_slice().to_vec())
Expand All @@ -196,6 +204,10 @@ impl Default for ByteString {
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a> From<&'a [u8]> for ByteString {
// /// Make a `ByteString` from a byte slice.
// ///
// /// ## Cost
// /// Allocates a new `Vec`
// #[inline]
// fn from(s: &'a [u8]) -> Self {
// ByteString(s.to_vec())
Expand All @@ -204,6 +216,7 @@ impl Default for ByteString {
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl From<Vec<u8>> for ByteString {
// /// Make a `ByteString` with `Vec<u8>` as inner
// #[inline]
// fn from(s: Vec<u8>) -> Self {
// ByteString(s)
Expand All @@ -212,6 +225,7 @@ impl Default for ByteString {

#[unstable(feature = "bstr", issue = "134915")]
impl From<ByteString> for Vec<u8> {
/// Return the inner `Vec` of the byte string.
#[inline]
fn from(s: ByteString) -> Self {
s.0
Expand All @@ -222,6 +236,10 @@ impl From<ByteString> for Vec<u8> {
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a> From<&'a str> for ByteString {
// /// Make a `ByteString` from a string slices bytes.
// ///
// /// ## Cost
// /// Allocates a new `Vec`
// #[inline]
// fn from(s: &'a str) -> Self {
// ByteString(s.as_bytes().to_vec())
Expand All @@ -230,6 +248,7 @@ impl From<ByteString> for Vec<u8> {
//
// #[unstable(feature = "bstr", issue = "134915")]
// impl From<String> for ByteString {
// /// Create a `ByteString` from a `String`s bytes
// #[inline]
// fn from(s: String) -> Self {
// ByteString(s.into_bytes())
Expand All @@ -238,6 +257,10 @@ impl From<ByteString> for Vec<u8> {

#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteStr> for ByteString {
/// Convert the `ByteStr` to a `Vec` then wrap it in a `ByteString`.
///
/// ## Cost
/// Allocates a new `Vec`
#[inline]
fn from(s: &'a ByteStr) -> Self {
ByteString(s.0.to_vec())
Expand All @@ -246,6 +269,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)
Expand All @@ -254,6 +278,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`.
#[inline]
fn from(s: &'a ByteString) -> Self {
Cow::Borrowed(s.as_bytestr())
Expand Down Expand Up @@ -599,6 +624,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)
Expand All @@ -607,6 +633,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.
#[inline]
fn from(s: Box<[u8]>) -> Box<ByteStr> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
Expand All @@ -616,6 +643,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`.
#[inline]
fn from(s: Box<ByteStr>) -> Box<[u8]> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
Expand All @@ -626,6 +654,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 a `Rc<[u8]>` from `Rc<ByteStr>`s raw.
#[inline]
fn from(s: Rc<[u8]>) -> Rc<ByteStr> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
Expand All @@ -636,6 +665,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 `Rc<[u8]>`s raw.
#[inline]
fn from(s: Rc<ByteStr>) -> Rc<[u8]> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
Expand All @@ -646,6 +676,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 a `Arc<ByteStr>` from `Arc<[u8]>`s raw.
#[inline]
fn from(s: Arc<[u8]>) -> Arc<ByteStr> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
Expand All @@ -656,6 +687,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<ByteStr>` from `Arc<[u8]>`s raw.
#[inline]
fn from(s: Arc<ByteStr>) -> Arc<[u8]> {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`.
Expand All @@ -675,6 +707,10 @@ impl_partial_eq_ord_cow!(&'a ByteStr, Cow<'a, [u8]>);
impl<'a> TryFrom<&'a ByteStr> for String {
type Error = core::str::Utf8Error;

/// Convert `ByteStr`s bytes to a UTF-8 `String`.
///
/// # Errors
/// If `ByteStr` is not valid UTF-8
#[inline]
fn try_from(s: &'a ByteStr) -> Result<Self, Self::Error> {
Ok(core::str::from_utf8(&s.0)?.into())
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub use realalloc::collections::TryReserveErrorKind;
)]
#[cfg(not(test))]
impl From<TryReserveErrorKind> for TryReserveError {
/// Wrap kind in `TryReserveError`.
#[inline]
fn from(kind: TryReserveErrorKind) -> Self {
Self { kind }
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
/// [`Vec<T>`]: crate::vec::Vec
/// [`VecDeque<T>`]: crate::collections::VecDeque
///
/// ## Cost
/// This conversion is guaranteed to run in *O*(1) time
/// and to not re-allocate the `Vec`'s buffer or allocate
/// any additional memory.
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub trait Wake {
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
/// Use a [`Wake`]-able type as a `Waker`.
///
/// ## Cost
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not agree with this new "Cost" heading

Copy link
Author

@TimTheBig TimTheBig Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so, I think it makes it more clear?

/// No heap allocations or atomic operations are used for this conversion.
fn from(waker: Arc<W>) -> Waker {
// SAFETY: This is safe because raw_waker safely constructs
Expand All @@ -121,6 +122,7 @@ impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
/// Use a `Wake`-able type as a `RawWaker`.
///
/// ## Cost
/// No heap allocations or atomic operations are used for this conversion.
fn from(waker: Arc<W>) -> RawWaker {
raw_waker(waker)
Expand Down Expand Up @@ -288,6 +290,7 @@ pub trait LocalWake {
impl<W: LocalWake + 'static> From<Rc<W>> for LocalWaker {
/// Use a `Wake`-able type as a `LocalWaker`.
///
/// ## Cost
/// No heap allocations or atomic operations are used for this conversion.
fn from(waker: Rc<W>) -> LocalWaker {
// SAFETY: This is safe because raw_waker safely constructs
Expand All @@ -300,6 +303,7 @@ impl<W: LocalWake + 'static> From<Rc<W>> for LocalWaker {
impl<W: LocalWake + 'static> From<Rc<W>> for RawWaker {
/// Use a `Wake`-able type as a `RawWaker`.
///
/// ## Cost
/// No heap allocations or atomic operations are used for this conversion.
fn from(waker: Rc<W>) -> RawWaker {
local_raw_waker(waker)
Expand Down
1 change: 1 addition & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl Error for TryFromSliceError {

#[stable(feature = "try_from_slice_error", since = "1.36.0")]
impl From<Infallible> for TryFromSliceError {
/// Convert `Infallible` into an error that can happen.
fn from(x: Infallible) -> TryFromSliceError {
match x {}
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/ascii/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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), "`")]
#[inline]
fn from(chr: AsciiChar) -> $ty {
chr as u8 as $ty
Expand Down
1 change: 1 addition & 0 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum Alignment {
#[doc(hidden)]
#[unstable(feature = "fmt_internals", reason = "internal to standard library", issue = "none")]
impl From<rt::Alignment> for Option<Alignment> {
/// Wrap the `rt::Alignment` in `Some` and `Unknown` gets converted to `None`.
fn from(value: rt::Alignment) -> Self {
match value {
rt::Alignment::Left => Some(Alignment::Left),
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/num/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ impl Error for TryFromIntError {

#[stable(feature = "try_from", since = "1.34.0")]
impl From<Infallible> for TryFromIntError {
/// Convert `Infallible` into an error that can happen.
#[inline]
fn from(x: Infallible) -> TryFromIntError {
match x {}
}
}

#[unstable(feature = "never_type", issue = "35121")]
impl From<!> for TryFromIntError {
/// Convert `!` into an error that can happen.
#[inline]
fn from(never: !) -> TryFromIntError {
// Match rather than coerce to make sure that code like
Expand Down
1 change: 1 addition & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ impl<T> From<NonZero<T>> for T
where
T: ZeroablePrimitive,
{
/// Returns the contained value as a primitive type.
#[inline]
fn from(nonzero: NonZero<T>) -> Self {
// Call `get` method to keep range information.
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl TryFrom<usize> for Alignment {

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl From<Alignment> for NonZero<usize> {
/// `Alignment` is non-zero so the inner value is returned
#[inline]
fn from(align: Alignment) -> NonZero<usize> {
align.as_nonzero()
Expand All @@ -197,6 +198,7 @@ impl From<Alignment> for NonZero<usize> {

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl From<Alignment> for usize {
/// Return the inner value of `Alignment`
#[inline]
fn from(align: Alignment) -> usize {
align.as_usize()
Expand Down
1 change: 1 addition & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,7 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {

#[unstable(feature = "ptr_internals", issue = "none")]
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
/// Return `Unique` cast to a `NonNull`.
#[inline]
fn from(unique: Unique<T>) -> Self {
unique.as_non_null_ptr()
Expand Down
6 changes: 6 additions & 0 deletions library/core/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ impl<T> IntoBounds<T> for Range<T> {

#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<Range<T>> for legacy::Range<T> {
/// Make a new `legacy::Range` with the same start and end as `Range`
#[inline]
fn from(value: Range<T>) -> Self {
Self { start: value.start, end: value.end }
}
}
#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<legacy::Range<T>> for Range<T> {
/// Make a new `Range` with the same start and end as `legacy::Range`
#[inline]
fn from(value: legacy::Range<T>) -> Self {
Self { start: value.start, end: value.end }
Expand Down Expand Up @@ -363,13 +365,15 @@ impl<T> IntoBounds<T> for RangeInclusive<T> {

#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
/// Make a new `legacy::RangeInclusive` with the same start and end as `RangeInclusive`
#[inline]
fn from(value: RangeInclusive<T>) -> Self {
Self::new(value.start, value.end)
}
}
#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
/// Make a new `RangeInclusive` with the same start and end as `legacy::RangeInclusive`
#[inline]
fn from(value: legacy::RangeInclusive<T>) -> Self {
assert!(
Expand Down Expand Up @@ -507,13 +511,15 @@ impl<T> IntoBounds<T> for RangeFrom<T> {

#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<RangeFrom<T>> for legacy::RangeFrom<T> {
/// Make a new `legacy::RangeFrom` with the same start as `RangeFrom`
#[inline]
fn from(value: RangeFrom<T>) -> Self {
Self { start: value.start }
}
}
#[unstable(feature = "new_range_api", issue = "125687")]
impl<T> From<legacy::RangeFrom<T>> for RangeFrom<T> {
/// Make a new `RangeFrom` with the same start as `legacy::RangeFrom`
#[inline]
fn from(value: legacy::RangeFrom<T>) -> Self {
Self { start: value.start }
Expand Down
1 change: 1 addition & 0 deletions library/core/src/sync/exclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
#[inline]
fn from(t: T) -> Self {
Self::new(t)
Expand Down
6 changes: 6 additions & 0 deletions library/portable-simd/crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ where
T: MaskElement,
LaneCount<N>: SupportedLaneCount,
{
/// Uses `from_array` to create a new `Mask`
#[inline]
fn from(array: [bool; N]) -> Self {
Self::from_array(array)
Expand All @@ -389,6 +390,10 @@ where
T: MaskElement,
LaneCount<N>: SupportedLaneCount,
{
/// Converts a SIMD mask to an array of bools.
///
/// ## Cost
/// Copies the bytes of the array
#[inline]
fn from(vector: Mask<T, N>) -> Self {
vector.to_array()
Expand Down Expand Up @@ -634,6 +639,7 @@ macro_rules! impl_from {
where
LaneCount<N>: SupportedLaneCount,
{
/// Casts the value into the other `Mask`
#[inline]
fn from(value: Mask<$from, N>) -> Self {
value.cast()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ where
T: MaskElement,
LaneCount<N>: SupportedLaneCount,
{
/// Return the `MaskElement` of the `Mask`
#[inline]
fn from(value: Mask<T, N>) -> Self {
value.0
Expand Down
2 changes: 2 additions & 0 deletions library/portable-simd/crates/core_simd/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ where
LaneCount<N>: SupportedLaneCount,
T: SimdElement,
{
/// Load the array into a new `Simd`
#[inline]
fn from(array: [T; N]) -> Self {
Self::from_array(array)
Expand All @@ -1058,6 +1059,7 @@ where
LaneCount<N>: SupportedLaneCount,
T: SimdElement,
{
/// Use `to_array` to store the `Simd` into a new array
#[inline]
fn from(vector: Simd<T, N>) -> Self {
vector.to_array()
Expand Down
1 change: 1 addition & 0 deletions library/portable-simd/crates/core_simd/src/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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), "`")]
#[inline]
fn from(value: $from) -> $to {
// Safety: transmuting between vectors is safe, but the caller of this macro
Expand Down
Loading
Loading