Auto merge of #105262 - eduardosm:more-inline-always, r=thomcc

Make some trivial functions `#[inline(always)]`

This is some kind of follow-up of PRs like https://github.com/rust-lang/rust/pull/85218, https://github.com/rust-lang/rust/pull/84061, https://github.com/rust-lang/rust/pull/87150. Functions that do very basic operations are made `#[inline(always)]` to avoid pessimizing them in debug builds when compared to using built-in operations directly.
This commit is contained in:
bors 2022-12-09 15:42:18 +00:00
commit f058493307
9 changed files with 33 additions and 25 deletions

View File

@ -176,7 +176,6 @@ pub struct AssertParamIsCopy<T: Copy + ?Sized> {
/// are implemented in `traits::SelectionContext::copy_clone_conditions()` /// are implemented in `traits::SelectionContext::copy_clone_conditions()`
/// in `rustc_trait_selection`. /// in `rustc_trait_selection`.
mod impls { mod impls {
use super::Clone; use super::Clone;
macro_rules! impl_clone { macro_rules! impl_clone {
@ -185,7 +184,7 @@ macro_rules! impl_clone {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_clone", issue = "91805")] #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
impl const Clone for $t { impl const Clone for $t {
#[inline] #[inline(always)]
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
@ -213,7 +212,7 @@ fn clone(&self) -> Self {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_clone", issue = "91805")] #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
impl<T: ?Sized> const Clone for *const T { impl<T: ?Sized> const Clone for *const T {
#[inline] #[inline(always)]
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
@ -222,7 +221,7 @@ fn clone(&self) -> Self {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_clone", issue = "91805")] #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
impl<T: ?Sized> const Clone for *mut T { impl<T: ?Sized> const Clone for *mut T {
#[inline] #[inline(always)]
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
@ -232,7 +231,7 @@ fn clone(&self) -> Self {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_clone", issue = "91805")] #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
impl<T: ?Sized> const Clone for &T { impl<T: ?Sized> const Clone for &T {
#[inline] #[inline(always)]
#[rustc_diagnostic_item = "noop_method_clone"] #[rustc_diagnostic_item = "noop_method_clone"]
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self

View File

@ -99,7 +99,7 @@
/// ``` /// ```
#[stable(feature = "convert_id", since = "1.33.0")] #[stable(feature = "convert_id", since = "1.33.0")]
#[rustc_const_stable(feature = "const_identity", since = "1.33.0")] #[rustc_const_stable(feature = "const_identity", since = "1.33.0")]
#[inline] #[inline(always)]
pub const fn identity<T>(x: T) -> T { pub const fn identity<T>(x: T) -> T {
x x
} }
@ -789,6 +789,7 @@ fn try_from(value: U) -> Result<Self, Self::Error> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for [T] { impl<T> AsRef<[T]> for [T] {
#[inline(always)]
fn as_ref(&self) -> &[T] { fn as_ref(&self) -> &[T] {
self self
} }
@ -796,6 +797,7 @@ fn as_ref(&self) -> &[T] {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsMut<[T]> for [T] { impl<T> AsMut<[T]> for [T] {
#[inline(always)]
fn as_mut(&mut self) -> &mut [T] { fn as_mut(&mut self) -> &mut [T] {
self self
} }
@ -803,7 +805,7 @@ fn as_mut(&mut self) -> &mut [T] {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<str> for str { impl AsRef<str> for str {
#[inline] #[inline(always)]
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
self self
} }
@ -811,7 +813,7 @@ fn as_ref(&self) -> &str {
#[stable(feature = "as_mut_str_for_str", since = "1.51.0")] #[stable(feature = "as_mut_str_for_str", since = "1.51.0")]
impl AsMut<str> for str { impl AsMut<str> for str {
#[inline] #[inline(always)]
fn as_mut(&mut self) -> &mut str { fn as_mut(&mut self) -> &mut str {
self self
} }

View File

@ -160,7 +160,7 @@
/// ``` /// ```
/// ///
/// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html /// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html
#[inline] #[inline(always)]
#[stable(feature = "renamed_spin_loop", since = "1.49.0")] #[stable(feature = "renamed_spin_loop", since = "1.49.0")]
pub fn spin_loop() { pub fn spin_loop() {
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
@ -345,6 +345,7 @@ pub const fn black_box<T>(dummy: T) -> T {
#[unstable(feature = "hint_must_use", issue = "94745")] #[unstable(feature = "hint_must_use", issue = "94745")]
#[rustc_const_unstable(feature = "hint_must_use", issue = "94745")] #[rustc_const_unstable(feature = "hint_must_use", issue = "94745")]
#[must_use] // <-- :) #[must_use] // <-- :)
#[inline(always)]
pub const fn must_use<T>(value: T) -> T { pub const fn must_use<T>(value: T) -> T {
value value
} }

View File

@ -45,7 +45,7 @@ pub const fn is_null(self) -> bool {
/// Casts to a pointer of another type. /// Casts to a pointer of another type.
#[stable(feature = "ptr_cast", since = "1.38.0")] #[stable(feature = "ptr_cast", since = "1.38.0")]
#[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")] #[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")]
#[inline] #[inline(always)]
pub const fn cast<U>(self) -> *const U { pub const fn cast<U>(self) -> *const U {
self as _ self as _
} }
@ -95,6 +95,7 @@ pub const fn with_metadata_of<U>(self, meta: *const U) -> *const U
/// refactored. /// refactored.
#[stable(feature = "ptr_const_cast", since = "1.65.0")] #[stable(feature = "ptr_const_cast", since = "1.65.0")]
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")] #[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
#[inline(always)]
pub const fn cast_mut(self) -> *mut T { pub const fn cast_mut(self) -> *mut T {
self as _ self as _
} }
@ -126,6 +127,7 @@ pub const fn cast_mut(self) -> *mut T {
note = "replaced by the `exposed_addr` method, or update your code \ note = "replaced by the `exposed_addr` method, or update your code \
to follow the strict provenance rules using its APIs" to follow the strict provenance rules using its APIs"
)] )]
#[inline(always)]
pub fn to_bits(self) -> usize pub fn to_bits(self) -> usize
where where
T: Sized, T: Sized,
@ -155,6 +157,7 @@ pub fn to_bits(self) -> usize
your code to follow the strict provenance rules using its APIs" your code to follow the strict provenance rules using its APIs"
)] )]
#[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function #[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function
#[inline(always)]
pub fn from_bits(bits: usize) -> Self pub fn from_bits(bits: usize) -> Self
where where
T: Sized, T: Sized,
@ -186,7 +189,7 @@ pub fn from_bits(bits: usize) -> Self
/// might change in the future (including possibly weakening this so it becomes wholly /// might change in the future (including possibly weakening this so it becomes wholly
/// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details. /// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details.
#[must_use] #[must_use]
#[inline] #[inline(always)]
#[unstable(feature = "strict_provenance", issue = "95228")] #[unstable(feature = "strict_provenance", issue = "95228")]
pub fn addr(self) -> usize pub fn addr(self) -> usize
where where
@ -223,7 +226,7 @@ pub fn addr(self) -> usize
/// ///
/// [`from_exposed_addr`]: from_exposed_addr /// [`from_exposed_addr`]: from_exposed_addr
#[must_use] #[must_use]
#[inline] #[inline(always)]
#[unstable(feature = "strict_provenance", issue = "95228")] #[unstable(feature = "strict_provenance", issue = "95228")]
pub fn expose_addr(self) -> usize pub fn expose_addr(self) -> usize
where where

View File

@ -613,7 +613,7 @@ pub const fn invalid_mut<T>(addr: usize) -> *mut T {
/// This API and its claimed semantics are part of the Strict Provenance experiment, see the /// This API and its claimed semantics are part of the Strict Provenance experiment, see the
/// [module documentation][crate::ptr] for details. /// [module documentation][crate::ptr] for details.
#[must_use] #[must_use]
#[inline] #[inline(always)]
#[unstable(feature = "strict_provenance", issue = "95228")] #[unstable(feature = "strict_provenance", issue = "95228")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[allow(fuzzy_provenance_casts)] // this *is* the strict provenance API one should use instead #[allow(fuzzy_provenance_casts)] // this *is* the strict provenance API one should use instead
@ -651,7 +651,7 @@ pub fn from_exposed_addr<T>(addr: usize) -> *const T
/// This API and its claimed semantics are part of the Strict Provenance experiment, see the /// This API and its claimed semantics are part of the Strict Provenance experiment, see the
/// [module documentation][crate::ptr] for details. /// [module documentation][crate::ptr] for details.
#[must_use] #[must_use]
#[inline] #[inline(always)]
#[unstable(feature = "strict_provenance", issue = "95228")] #[unstable(feature = "strict_provenance", issue = "95228")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[allow(fuzzy_provenance_casts)] // this *is* the strict provenance API one should use instead #[allow(fuzzy_provenance_casts)] // this *is* the strict provenance API one should use instead
@ -1801,7 +1801,7 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
/// assert!(!std::ptr::eq(&a[0..2], &a[1..3])); /// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
/// ``` /// ```
#[stable(feature = "ptr_eq", since = "1.17.0")] #[stable(feature = "ptr_eq", since = "1.17.0")]
#[inline] #[inline(always)]
pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool { pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
a == b a == b
} }

View File

@ -100,6 +100,7 @@ pub const fn with_metadata_of<U>(self, meta: *const U) -> *mut U
/// [`cast_mut`]: #method.cast_mut /// [`cast_mut`]: #method.cast_mut
#[stable(feature = "ptr_const_cast", since = "1.65.0")] #[stable(feature = "ptr_const_cast", since = "1.65.0")]
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")] #[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
#[inline(always)]
pub const fn cast_const(self) -> *const T { pub const fn cast_const(self) -> *const T {
self as _ self as _
} }
@ -132,6 +133,7 @@ pub const fn cast_const(self) -> *const T {
note = "replaced by the `exposed_addr` method, or update your code \ note = "replaced by the `exposed_addr` method, or update your code \
to follow the strict provenance rules using its APIs" to follow the strict provenance rules using its APIs"
)] )]
#[inline(always)]
pub fn to_bits(self) -> usize pub fn to_bits(self) -> usize
where where
T: Sized, T: Sized,
@ -161,6 +163,7 @@ pub fn to_bits(self) -> usize
update your code to follow the strict provenance rules using its APIs" update your code to follow the strict provenance rules using its APIs"
)] )]
#[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function #[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function
#[inline(always)]
pub fn from_bits(bits: usize) -> Self pub fn from_bits(bits: usize) -> Self
where where
T: Sized, T: Sized,
@ -192,7 +195,7 @@ pub fn from_bits(bits: usize) -> Self
/// might change in the future (including possibly weakening this so it becomes wholly /// might change in the future (including possibly weakening this so it becomes wholly
/// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details. /// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details.
#[must_use] #[must_use]
#[inline] #[inline(always)]
#[unstable(feature = "strict_provenance", issue = "95228")] #[unstable(feature = "strict_provenance", issue = "95228")]
pub fn addr(self) -> usize pub fn addr(self) -> usize
where where
@ -229,7 +232,7 @@ pub fn addr(self) -> usize
/// ///
/// [`from_exposed_addr_mut`]: from_exposed_addr_mut /// [`from_exposed_addr_mut`]: from_exposed_addr_mut
#[must_use] #[must_use]
#[inline] #[inline(always)]
#[unstable(feature = "strict_provenance", issue = "95228")] #[unstable(feature = "strict_provenance", issue = "95228")]
pub fn expose_addr(self) -> usize pub fn expose_addr(self) -> usize
where where

View File

@ -330,7 +330,7 @@ pub fn map_addr(self, f: impl FnOnce(NonZeroUsize) -> NonZeroUsize) -> Self
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")] #[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
#[must_use] #[must_use]
#[inline] #[inline(always)]
pub const fn as_ptr(self) -> *mut T { pub const fn as_ptr(self) -> *mut T {
self.pointer as *mut T self.pointer as *mut T
} }
@ -378,7 +378,7 @@ pub const fn as_ptr(self) -> *mut T {
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")] #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[must_use] #[must_use]
#[inline] #[inline(always)]
pub const unsafe fn as_ref<'a>(&self) -> &'a T { pub const unsafe fn as_ref<'a>(&self) -> &'a T {
// SAFETY: the caller must guarantee that `self` meets all the // SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference. // requirements for a reference.
@ -429,7 +429,7 @@ pub const fn as_ptr(self) -> *mut T {
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")] #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[must_use] #[must_use]
#[inline] #[inline(always)]
pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T { pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
// SAFETY: the caller must guarantee that `self` meets all the // SAFETY: the caller must guarantee that `self` meets all the
// requirements for a mutable reference. // requirements for a mutable reference.
@ -703,7 +703,7 @@ pub const fn as_mut_ptr(self) -> *mut T {
#[stable(feature = "nonnull", since = "1.25.0")] #[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_clone", issue = "91805")] #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
impl<T: ?Sized> const Clone for NonNull<T> { impl<T: ?Sized> const Clone for NonNull<T> {
#[inline] #[inline(always)]
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }

View File

@ -465,7 +465,7 @@ pub const fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
/// [`as_mut_ptr`]: slice::as_mut_ptr /// [`as_mut_ptr`]: slice::as_mut_ptr
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")] #[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
#[inline] #[inline(always)]
#[must_use] #[must_use]
pub const fn as_ptr(&self) -> *const T { pub const fn as_ptr(&self) -> *const T {
self as *const [T] as *const T self as *const [T] as *const T
@ -495,7 +495,7 @@ pub const fn as_ptr(&self) -> *const T {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[rustc_allow_const_fn_unstable(const_mut_refs)] #[rustc_allow_const_fn_unstable(const_mut_refs)]
#[inline] #[inline(always)]
#[must_use] #[must_use]
pub const fn as_mut_ptr(&mut self) -> *mut T { pub const fn as_mut_ptr(&mut self) -> *mut T {
self as *mut [T] as *mut T self as *mut [T] as *mut T

View File

@ -396,7 +396,7 @@ pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")] #[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
#[must_use] #[must_use]
#[inline] #[inline(always)]
pub const fn as_ptr(&self) -> *const u8 { pub const fn as_ptr(&self) -> *const u8 {
self as *const str as *const u8 self as *const str as *const u8
} }
@ -411,7 +411,7 @@ pub const fn as_ptr(&self) -> *const u8 {
/// modified in a way that it remains valid UTF-8. /// modified in a way that it remains valid UTF-8.
#[stable(feature = "str_as_mut_ptr", since = "1.36.0")] #[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
#[must_use] #[must_use]
#[inline] #[inline(always)]
pub fn as_mut_ptr(&mut self) -> *mut u8 { pub fn as_mut_ptr(&mut self) -> *mut u8 {
self as *mut str as *mut u8 self as *mut str as *mut u8
} }