Skip to content

Commit f058493

Browse files
committed
Auto merge of rust-lang#105262 - eduardosm:more-inline-always, r=thomcc
Make some trivial functions `#[inline(always)]` This is some kind of follow-up of PRs like rust-lang#85218, rust-lang#84061, rust-lang#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.
2 parents e10201c + 00e7b54 commit f058493

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

library/core/src/clone.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ pub struct AssertParamIsCopy<T: Copy + ?Sized> {
176176
/// are implemented in `traits::SelectionContext::copy_clone_conditions()`
177177
/// in `rustc_trait_selection`.
178178
mod impls {
179-
180179
use super::Clone;
181180

182181
macro_rules! impl_clone {
@@ -185,7 +184,7 @@ mod impls {
185184
#[stable(feature = "rust1", since = "1.0.0")]
186185
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
187186
impl const Clone for $t {
188-
#[inline]
187+
#[inline(always)]
189188
fn clone(&self) -> Self {
190189
*self
191190
}
@@ -213,7 +212,7 @@ mod impls {
213212
#[stable(feature = "rust1", since = "1.0.0")]
214213
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
215214
impl<T: ?Sized> const Clone for *const T {
216-
#[inline]
215+
#[inline(always)]
217216
fn clone(&self) -> Self {
218217
*self
219218
}
@@ -222,7 +221,7 @@ mod impls {
222221
#[stable(feature = "rust1", since = "1.0.0")]
223222
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
224223
impl<T: ?Sized> const Clone for *mut T {
225-
#[inline]
224+
#[inline(always)]
226225
fn clone(&self) -> Self {
227226
*self
228227
}
@@ -232,7 +231,7 @@ mod impls {
232231
#[stable(feature = "rust1", since = "1.0.0")]
233232
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
234233
impl<T: ?Sized> const Clone for &T {
235-
#[inline]
234+
#[inline(always)]
236235
#[rustc_diagnostic_item = "noop_method_clone"]
237236
fn clone(&self) -> Self {
238237
*self

library/core/src/convert/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub use num::FloatToInt;
9999
/// ```
100100
#[stable(feature = "convert_id", since = "1.33.0")]
101101
#[rustc_const_stable(feature = "const_identity", since = "1.33.0")]
102-
#[inline]
102+
#[inline(always)]
103103
pub const fn identity<T>(x: T) -> T {
104104
x
105105
}
@@ -789,29 +789,31 @@ where
789789

790790
#[stable(feature = "rust1", since = "1.0.0")]
791791
impl<T> AsRef<[T]> for [T] {
792+
#[inline(always)]
792793
fn as_ref(&self) -> &[T] {
793794
self
794795
}
795796
}
796797

797798
#[stable(feature = "rust1", since = "1.0.0")]
798799
impl<T> AsMut<[T]> for [T] {
800+
#[inline(always)]
799801
fn as_mut(&mut self) -> &mut [T] {
800802
self
801803
}
802804
}
803805

804806
#[stable(feature = "rust1", since = "1.0.0")]
805807
impl AsRef<str> for str {
806-
#[inline]
808+
#[inline(always)]
807809
fn as_ref(&self) -> &str {
808810
self
809811
}
810812
}
811813

812814
#[stable(feature = "as_mut_str_for_str", since = "1.51.0")]
813815
impl AsMut<str> for str {
814-
#[inline]
816+
#[inline(always)]
815817
fn as_mut(&mut self) -> &mut str {
816818
self
817819
}

library/core/src/hint.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub const unsafe fn unreachable_unchecked() -> ! {
160160
/// ```
161161
///
162162
/// [`thread::yield_now`]: ../../std/thread/fn.yield_now.html
163-
#[inline]
163+
#[inline(always)]
164164
#[stable(feature = "renamed_spin_loop", since = "1.49.0")]
165165
pub fn spin_loop() {
166166
#[cfg(target_arch = "x86")]
@@ -345,6 +345,7 @@ pub const fn black_box<T>(dummy: T) -> T {
345345
#[unstable(feature = "hint_must_use", issue = "94745")]
346346
#[rustc_const_unstable(feature = "hint_must_use", issue = "94745")]
347347
#[must_use] // <-- :)
348+
#[inline(always)]
348349
pub const fn must_use<T>(value: T) -> T {
349350
value
350351
}

library/core/src/ptr/const_ptr.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<T: ?Sized> *const T {
4545
/// Casts to a pointer of another type.
4646
#[stable(feature = "ptr_cast", since = "1.38.0")]
4747
#[rustc_const_stable(feature = "const_ptr_cast", since = "1.38.0")]
48-
#[inline]
48+
#[inline(always)]
4949
pub const fn cast<U>(self) -> *const U {
5050
self as _
5151
}
@@ -95,6 +95,7 @@ impl<T: ?Sized> *const T {
9595
/// refactored.
9696
#[stable(feature = "ptr_const_cast", since = "1.65.0")]
9797
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
98+
#[inline(always)]
9899
pub const fn cast_mut(self) -> *mut T {
99100
self as _
100101
}
@@ -126,6 +127,7 @@ impl<T: ?Sized> *const T {
126127
note = "replaced by the `exposed_addr` method, or update your code \
127128
to follow the strict provenance rules using its APIs"
128129
)]
130+
#[inline(always)]
129131
pub fn to_bits(self) -> usize
130132
where
131133
T: Sized,
@@ -155,6 +157,7 @@ impl<T: ?Sized> *const T {
155157
your code to follow the strict provenance rules using its APIs"
156158
)]
157159
#[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function
160+
#[inline(always)]
158161
pub fn from_bits(bits: usize) -> Self
159162
where
160163
T: Sized,
@@ -186,7 +189,7 @@ impl<T: ?Sized> *const T {
186189
/// might change in the future (including possibly weakening this so it becomes wholly
187190
/// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details.
188191
#[must_use]
189-
#[inline]
192+
#[inline(always)]
190193
#[unstable(feature = "strict_provenance", issue = "95228")]
191194
pub fn addr(self) -> usize
192195
where
@@ -223,7 +226,7 @@ impl<T: ?Sized> *const T {
223226
///
224227
/// [`from_exposed_addr`]: from_exposed_addr
225228
#[must_use]
226-
#[inline]
229+
#[inline(always)]
227230
#[unstable(feature = "strict_provenance", issue = "95228")]
228231
pub fn expose_addr(self) -> usize
229232
where

library/core/src/ptr/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ pub const fn invalid_mut<T>(addr: usize) -> *mut T {
613613
/// This API and its claimed semantics are part of the Strict Provenance experiment, see the
614614
/// [module documentation][crate::ptr] for details.
615615
#[must_use]
616-
#[inline]
616+
#[inline(always)]
617617
#[unstable(feature = "strict_provenance", issue = "95228")]
618618
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
619619
#[allow(fuzzy_provenance_casts)] // this *is* the strict provenance API one should use instead
@@ -651,7 +651,7 @@ where
651651
/// This API and its claimed semantics are part of the Strict Provenance experiment, see the
652652
/// [module documentation][crate::ptr] for details.
653653
#[must_use]
654-
#[inline]
654+
#[inline(always)]
655655
#[unstable(feature = "strict_provenance", issue = "95228")]
656656
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
657657
#[allow(fuzzy_provenance_casts)] // this *is* the strict provenance API one should use instead
@@ -1801,7 +1801,7 @@ pub(crate) const unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usiz
18011801
/// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
18021802
/// ```
18031803
#[stable(feature = "ptr_eq", since = "1.17.0")]
1804-
#[inline]
1804+
#[inline(always)]
18051805
pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
18061806
a == b
18071807
}

library/core/src/ptr/mut_ptr.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl<T: ?Sized> *mut T {
100100
/// [`cast_mut`]: #method.cast_mut
101101
#[stable(feature = "ptr_const_cast", since = "1.65.0")]
102102
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
103+
#[inline(always)]
103104
pub const fn cast_const(self) -> *const T {
104105
self as _
105106
}
@@ -132,6 +133,7 @@ impl<T: ?Sized> *mut T {
132133
note = "replaced by the `exposed_addr` method, or update your code \
133134
to follow the strict provenance rules using its APIs"
134135
)]
136+
#[inline(always)]
135137
pub fn to_bits(self) -> usize
136138
where
137139
T: Sized,
@@ -161,6 +163,7 @@ impl<T: ?Sized> *mut T {
161163
update your code to follow the strict provenance rules using its APIs"
162164
)]
163165
#[allow(fuzzy_provenance_casts)] // this is an unstable and semi-deprecated cast function
166+
#[inline(always)]
164167
pub fn from_bits(bits: usize) -> Self
165168
where
166169
T: Sized,
@@ -192,7 +195,7 @@ impl<T: ?Sized> *mut T {
192195
/// might change in the future (including possibly weakening this so it becomes wholly
193196
/// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details.
194197
#[must_use]
195-
#[inline]
198+
#[inline(always)]
196199
#[unstable(feature = "strict_provenance", issue = "95228")]
197200
pub fn addr(self) -> usize
198201
where
@@ -229,7 +232,7 @@ impl<T: ?Sized> *mut T {
229232
///
230233
/// [`from_exposed_addr_mut`]: from_exposed_addr_mut
231234
#[must_use]
232-
#[inline]
235+
#[inline(always)]
233236
#[unstable(feature = "strict_provenance", issue = "95228")]
234237
pub fn expose_addr(self) -> usize
235238
where

library/core/src/ptr/non_null.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<T: ?Sized> NonNull<T> {
330330
#[stable(feature = "nonnull", since = "1.25.0")]
331331
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
332332
#[must_use]
333-
#[inline]
333+
#[inline(always)]
334334
pub const fn as_ptr(self) -> *mut T {
335335
self.pointer as *mut T
336336
}
@@ -378,7 +378,7 @@ impl<T: ?Sized> NonNull<T> {
378378
#[stable(feature = "nonnull", since = "1.25.0")]
379379
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
380380
#[must_use]
381-
#[inline]
381+
#[inline(always)]
382382
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
383383
// SAFETY: the caller must guarantee that `self` meets all the
384384
// requirements for a reference.
@@ -429,7 +429,7 @@ impl<T: ?Sized> NonNull<T> {
429429
#[stable(feature = "nonnull", since = "1.25.0")]
430430
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
431431
#[must_use]
432-
#[inline]
432+
#[inline(always)]
433433
pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
434434
// SAFETY: the caller must guarantee that `self` meets all the
435435
// requirements for a mutable reference.
@@ -703,7 +703,7 @@ impl<T> NonNull<[T]> {
703703
#[stable(feature = "nonnull", since = "1.25.0")]
704704
#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
705705
impl<T: ?Sized> const Clone for NonNull<T> {
706-
#[inline]
706+
#[inline(always)]
707707
fn clone(&self) -> Self {
708708
*self
709709
}

library/core/src/slice/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl<T> [T] {
465465
/// [`as_mut_ptr`]: slice::as_mut_ptr
466466
#[stable(feature = "rust1", since = "1.0.0")]
467467
#[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
468-
#[inline]
468+
#[inline(always)]
469469
#[must_use]
470470
pub const fn as_ptr(&self) -> *const T {
471471
self as *const [T] as *const T
@@ -495,7 +495,7 @@ impl<T> [T] {
495495
#[stable(feature = "rust1", since = "1.0.0")]
496496
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
497497
#[rustc_allow_const_fn_unstable(const_mut_refs)]
498-
#[inline]
498+
#[inline(always)]
499499
#[must_use]
500500
pub const fn as_mut_ptr(&mut self) -> *mut T {
501501
self as *mut [T] as *mut T

library/core/src/str/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl str {
396396
#[stable(feature = "rust1", since = "1.0.0")]
397397
#[rustc_const_stable(feature = "rustc_str_as_ptr", since = "1.32.0")]
398398
#[must_use]
399-
#[inline]
399+
#[inline(always)]
400400
pub const fn as_ptr(&self) -> *const u8 {
401401
self as *const str as *const u8
402402
}
@@ -411,7 +411,7 @@ impl str {
411411
/// modified in a way that it remains valid UTF-8.
412412
#[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
413413
#[must_use]
414-
#[inline]
414+
#[inline(always)]
415415
pub fn as_mut_ptr(&mut self) -> *mut u8 {
416416
self as *mut str as *mut u8
417417
}

0 commit comments

Comments
 (0)