mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
Add #[must_use] to from_value conversions
This commit is contained in:
parent
6928fafe06
commit
cf2bcd10ed
@ -595,6 +595,7 @@ impl str {
|
||||
/// assert_eq!("☺", &*smile);
|
||||
/// ```
|
||||
#[stable(feature = "str_box_extras", since = "1.20.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
|
||||
unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
|
||||
|
@ -761,6 +761,7 @@ impl String {
|
||||
/// assert_eq!("💖", sparkle_heart);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
|
||||
String { vec: bytes }
|
||||
|
@ -94,6 +94,7 @@ impl Layout {
|
||||
/// [`Layout::from_size_align`].
|
||||
#[stable(feature = "alloc_layout", since = "1.28.0")]
|
||||
#[rustc_const_stable(feature = "alloc_layout", since = "1.36.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
|
||||
// SAFETY: the caller must ensure that `align` is greater than zero.
|
||||
|
@ -48,6 +48,7 @@ use super::MAX;
|
||||
/// assert_eq!(None, c);
|
||||
/// ```
|
||||
#[doc(alias = "chr")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn from_u32(i: u32) -> Option<char> {
|
||||
@ -88,6 +89,7 @@ pub fn from_u32(i: u32) -> Option<char> {
|
||||
/// assert_eq!('❤', c);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "char_from_unchecked", since = "1.5.0")]
|
||||
pub unsafe fn from_u32_unchecked(i: u32) -> char {
|
||||
// SAFETY: the caller must guarantee that `i` is a valid char value.
|
||||
@ -319,6 +321,7 @@ impl fmt::Display for CharTryFromError {
|
||||
/// let c = char::from_digit(1, 37);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub fn from_digit(num: u32, radix: u32) -> Option<char> {
|
||||
if radix > 36 {
|
||||
|
@ -136,6 +136,7 @@ impl char {
|
||||
/// assert_eq!(None, c);
|
||||
/// ```
|
||||
#[stable(feature = "assoc_char_funcs", since = "1.52.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn from_u32(i: u32) -> Option<char> {
|
||||
super::convert::from_u32(i)
|
||||
@ -177,6 +178,7 @@ impl char {
|
||||
/// assert_eq!('❤', c);
|
||||
/// ```
|
||||
#[stable(feature = "assoc_char_funcs", since = "1.52.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub unsafe fn from_u32_unchecked(i: u32) -> char {
|
||||
// SAFETY: the safety contract must be upheld by the caller.
|
||||
@ -230,9 +232,10 @@ impl char {
|
||||
/// use std::char;
|
||||
///
|
||||
/// // this panics
|
||||
/// char::from_digit(1, 37);
|
||||
/// let _c = char::from_digit(1, 37);
|
||||
/// ```
|
||||
#[stable(feature = "assoc_char_funcs", since = "1.52.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn from_digit(num: u32, radix: u32) -> Option<char> {
|
||||
super::convert::from_digit(num, radix)
|
||||
|
@ -786,6 +786,7 @@ impl f32 {
|
||||
/// ```
|
||||
#[stable(feature = "float_bits_conv", since = "1.20.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_bits(v: u32) -> Self {
|
||||
// SAFETY: `u32` is a plain old datatype so we can always transmute from it
|
||||
@ -864,6 +865,7 @@ impl f32 {
|
||||
/// ```
|
||||
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
|
||||
Self::from_bits(u32::from_be_bytes(bytes))
|
||||
@ -879,6 +881,7 @@ impl f32 {
|
||||
/// ```
|
||||
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
|
||||
Self::from_bits(u32::from_le_bytes(bytes))
|
||||
@ -905,6 +908,7 @@ impl f32 {
|
||||
/// ```
|
||||
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
|
||||
Self::from_bits(u32::from_ne_bytes(bytes))
|
||||
|
@ -800,6 +800,7 @@ impl f64 {
|
||||
/// ```
|
||||
#[stable(feature = "float_bits_conv", since = "1.20.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_bits(v: u64) -> Self {
|
||||
// SAFETY: `u64` is a plain old datatype so we can always transmute from it
|
||||
@ -878,6 +879,7 @@ impl f64 {
|
||||
/// ```
|
||||
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
|
||||
Self::from_bits(u64::from_be_bytes(bytes))
|
||||
@ -893,6 +895,7 @@ impl f64 {
|
||||
/// ```
|
||||
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
|
||||
Self::from_bits(u64::from_le_bytes(bytes))
|
||||
@ -919,6 +922,7 @@ impl f64 {
|
||||
/// ```
|
||||
#[stable(feature = "float_to_from_bytes", since = "1.40.0")]
|
||||
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
|
||||
Self::from_bits(u64::from_ne_bytes(bytes))
|
||||
|
@ -282,6 +282,7 @@ macro_rules! int_impl {
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_be(x: Self) -> Self {
|
||||
#[cfg(target_endian = "big")]
|
||||
@ -313,6 +314,7 @@ macro_rules! int_impl {
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_le(x: Self) -> Self {
|
||||
#[cfg(target_endian = "little")]
|
||||
@ -2620,6 +2622,7 @@ macro_rules! int_impl {
|
||||
/// ```
|
||||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
@ -2650,6 +2653,7 @@ macro_rules! int_impl {
|
||||
/// ```
|
||||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
@ -2691,6 +2695,7 @@ macro_rules! int_impl {
|
||||
/// ```
|
||||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
|
||||
#[must_use]
|
||||
// SAFETY: const sound because integers are plain old datatypes so we can always
|
||||
// transmute to them
|
||||
#[inline]
|
||||
|
@ -644,6 +644,7 @@ macro_rules! saturating_int_impl {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "saturating_int_impl", issue = "87920")]
|
||||
pub const fn from_be(x: Self) -> Self {
|
||||
Saturating(<$t>::from_be(x.0))
|
||||
@ -671,6 +672,7 @@ macro_rules! saturating_int_impl {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "saturating_int_impl", issue = "87920")]
|
||||
pub const fn from_le(x: Self) -> Self {
|
||||
Saturating(<$t>::from_le(x.0))
|
||||
|
@ -285,6 +285,7 @@ macro_rules! uint_impl {
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
|
||||
#[must_use]
|
||||
#[inline(always)]
|
||||
pub const fn from_be(x: Self) -> Self {
|
||||
#[cfg(target_endian = "big")]
|
||||
@ -317,6 +318,7 @@ macro_rules! uint_impl {
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
|
||||
#[must_use]
|
||||
#[inline(always)]
|
||||
pub const fn from_le(x: Self) -> Self {
|
||||
#[cfg(target_endian = "little")]
|
||||
@ -2278,6 +2280,7 @@ macro_rules! uint_impl {
|
||||
/// ```
|
||||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_be(Self::from_ne_bytes(bytes))
|
||||
@ -2308,6 +2311,7 @@ macro_rules! uint_impl {
|
||||
/// ```
|
||||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
|
||||
Self::from_le(Self::from_ne_bytes(bytes))
|
||||
@ -2349,6 +2353,7 @@ macro_rules! uint_impl {
|
||||
/// ```
|
||||
#[stable(feature = "int_to_from_bytes", since = "1.32.0")]
|
||||
#[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
|
||||
#[must_use]
|
||||
// SAFETY: const sound because integers are plain old datatypes so we can always
|
||||
// transmute to them
|
||||
#[inline]
|
||||
|
@ -638,6 +638,7 @@ macro_rules! wrapping_int_impl {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
|
||||
pub const fn from_be(x: Self) -> Self {
|
||||
Wrapping(<$t>::from_be(x.0))
|
||||
@ -665,6 +666,7 @@ macro_rules! wrapping_int_impl {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
|
||||
pub const fn from_le(x: Self) -> Self {
|
||||
Wrapping(<$t>::from_le(x.0))
|
||||
|
@ -155,6 +155,7 @@ pub fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> {
|
||||
/// assert_eq!("💖", sparkle_heart);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")]
|
||||
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
|
||||
@ -181,6 +182,7 @@ pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
|
||||
/// assert_eq!("💖", heart);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "str_mut_extras", since = "1.20.0")]
|
||||
pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
|
||||
// SAFETY: the caller must guarantee that the bytes `v`
|
||||
|
@ -12,10 +12,12 @@ pub struct Utf8Lossy {
|
||||
}
|
||||
|
||||
impl Utf8Lossy {
|
||||
#[must_use]
|
||||
pub fn from_str(s: &str) -> &Utf8Lossy {
|
||||
Utf8Lossy::from_bytes(s.as_bytes())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
|
||||
// SAFETY: Both use the same memory layout, and UTF-8 correctness isn't required.
|
||||
unsafe { mem::transmute(bytes) }
|
||||
|
@ -158,6 +158,7 @@ pub struct Context<'a> {
|
||||
impl<'a> Context<'a> {
|
||||
/// Create a new `Context` from a `&Waker`.
|
||||
#[stable(feature = "futures_api", since = "1.36.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn from_waker(waker: &'a Waker) -> Self {
|
||||
Context { waker, _marker: PhantomData }
|
||||
@ -251,6 +252,7 @@ impl Waker {
|
||||
/// in [`RawWaker`]'s and [`RawWakerVTable`]'s documentation is not upheld.
|
||||
/// Therefore this method is unsafe.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "futures_api", since = "1.36.0")]
|
||||
pub unsafe fn from_raw(waker: RawWaker) -> Waker {
|
||||
Waker { waker }
|
||||
|
@ -203,6 +203,7 @@ impl Duration {
|
||||
/// assert_eq!(0, duration.subsec_nanos());
|
||||
/// ```
|
||||
#[stable(feature = "duration", since = "1.3.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
||||
pub const fn from_secs(secs: u64) -> Duration {
|
||||
@ -222,6 +223,7 @@ impl Duration {
|
||||
/// assert_eq!(569_000_000, duration.subsec_nanos());
|
||||
/// ```
|
||||
#[stable(feature = "duration", since = "1.3.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
||||
pub const fn from_millis(millis: u64) -> Duration {
|
||||
@ -244,6 +246,7 @@ impl Duration {
|
||||
/// assert_eq!(2000, duration.subsec_nanos());
|
||||
/// ```
|
||||
#[stable(feature = "duration_from_micros", since = "1.27.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
||||
pub const fn from_micros(micros: u64) -> Duration {
|
||||
@ -266,6 +269,7 @@ impl Duration {
|
||||
/// assert_eq!(123, duration.subsec_nanos());
|
||||
/// ```
|
||||
#[stable(feature = "duration_extras", since = "1.27.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
|
||||
pub const fn from_nanos(nanos: u64) -> Duration {
|
||||
@ -692,6 +696,7 @@ impl Duration {
|
||||
/// assert_eq!(dur, Duration::new(2, 700_000_000));
|
||||
/// ```
|
||||
#[stable(feature = "duration_float", since = "1.38.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
|
||||
pub const fn from_secs_f64(secs: f64) -> Duration {
|
||||
@ -753,6 +758,7 @@ impl Duration {
|
||||
/// assert_eq!(dur, Duration::new(2, 700_000_000));
|
||||
/// ```
|
||||
#[stable(feature = "duration_float", since = "1.38.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
#[rustc_const_unstable(feature = "duration_consts_2", issue = "72440")]
|
||||
pub const fn from_secs_f32(secs: f32) -> Duration {
|
||||
|
@ -425,6 +425,7 @@ impl CString {
|
||||
/// let c_string = CString::from_vec_unchecked(raw);
|
||||
/// }
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
|
||||
v.reserve_exact(1);
|
||||
@ -476,6 +477,7 @@ impl CString {
|
||||
/// let c_string = CString::from_raw(raw);
|
||||
/// }
|
||||
/// ```
|
||||
#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
|
||||
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
||||
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
|
||||
// SAFETY: This is called with a pointer that was obtained from a call
|
||||
@ -701,6 +703,7 @@ impl CString {
|
||||
/// unsafe { CString::from_vec_unchecked(b"abc".to_vec()) }
|
||||
/// );
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[unstable(feature = "cstring_from_vec_with_nul", issue = "73179")]
|
||||
pub unsafe fn from_vec_with_nul_unchecked(v: Vec<u8>) -> Self {
|
||||
Self { inner: v.into_boxed_slice() }
|
||||
@ -1162,6 +1165,7 @@ impl CStr {
|
||||
/// }
|
||||
/// # }
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
|
||||
// SAFETY: The caller has provided a pointer that points to a valid C
|
||||
@ -1244,6 +1248,7 @@ impl CStr {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[must_use]
|
||||
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
|
||||
#[rustc_const_unstable(feature = "const_cstr_unchecked", issue = "none")]
|
||||
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
|
||||
|
@ -473,6 +473,7 @@ impl Error {
|
||||
/// # }
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub fn from_raw_os_error(code: i32) -> Error {
|
||||
Error { repr: Repr::Os(code) }
|
||||
|
Loading…
Reference in New Issue
Block a user