Skip to content

Commit e50157c

Browse files
committed
Add #[inline] to small functions in core
1 parent cdca82c commit e50157c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+306
-1
lines changed

library/core/src/alloc/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::ptr::{Alignment, NonNull};
1616
//
1717
// * https://github.com/rust-lang/rust/pull/72189
1818
// * https://github.com/rust-lang/rust/pull/79827
19+
#[inline]
1920
const fn size_align<T>() -> (usize, usize) {
2021
(mem::size_of::<T>(), mem::align_of::<T>())
2122
}

library/core/src/array/iter.rs

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl<T, const N: usize> IntoIterator for [T; N] {
5353
/// 2021 edition -- see the [array] Editions section for more information.
5454
///
5555
/// [array]: prim@array
56+
#[inline]
5657
fn into_iter(self) -> Self::IntoIter {
5758
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
5859
// promise:
@@ -76,6 +77,7 @@ impl<T, const N: usize> IntoIter<T, N> {
7677
/// Creates a new iterator over the given `array`.
7778
#[stable(feature = "array_value_iter", since = "1.51.0")]
7879
#[deprecated(since = "1.59.0", note = "use `IntoIterator::into_iter` instead")]
80+
#[inline]
7981
pub fn new(array: [T; N]) -> Self {
8082
IntoIterator::into_iter(array)
8183
}
@@ -138,6 +140,7 @@ impl<T, const N: usize> IntoIter<T, N> {
138140
/// ```
139141
#[unstable(feature = "array_into_iter_constructors", issue = "91583")]
140142
#[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
143+
#[inline]
141144
pub const unsafe fn new_unchecked(
142145
buffer: [MaybeUninit<T>; N],
143146
initialized: Range<usize>,
@@ -356,9 +359,11 @@ impl<T, const N: usize> Drop for IntoIter<T, N> {
356359

357360
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
358361
impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
362+
#[inline]
359363
fn len(&self) -> usize {
360364
self.alive.len()
361365
}
366+
#[inline]
362367
fn is_empty(&self) -> bool {
363368
self.alive.is_empty()
364369
}

library/core/src/array/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ where
113113
/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
114114
#[stable(feature = "array_from_ref", since = "1.53.0")]
115115
#[rustc_const_stable(feature = "const_array_from_ref_shared", since = "1.63.0")]
116+
#[inline]
116117
pub const fn from_ref<T>(s: &T) -> &[T; 1] {
117118
// SAFETY: Converting `&T` to `&[T; 1]` is sound.
118119
unsafe { &*(s as *const T).cast::<[T; 1]>() }
@@ -121,6 +122,7 @@ pub const fn from_ref<T>(s: &T) -> &[T; 1] {
121122
/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
122123
#[stable(feature = "array_from_ref", since = "1.53.0")]
123124
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")]
125+
#[inline]
124126
pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
125127
// SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
126128
unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
@@ -143,13 +145,15 @@ impl fmt::Display for TryFromSliceError {
143145
#[stable(feature = "try_from", since = "1.34.0")]
144146
impl Error for TryFromSliceError {
145147
#[allow(deprecated)]
148+
#[inline]
146149
fn description(&self) -> &str {
147150
"could not convert slice to array"
148151
}
149152
}
150153

151154
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
152155
impl From<Infallible> for TryFromSliceError {
156+
#[inline]
153157
fn from(x: Infallible) -> TryFromSliceError {
154158
match x {}
155159
}
@@ -173,13 +177,15 @@ impl<T, const N: usize> AsMut<[T]> for [T; N] {
173177

174178
#[stable(feature = "array_borrow", since = "1.4.0")]
175179
impl<T, const N: usize> Borrow<[T]> for [T; N] {
180+
#[inline]
176181
fn borrow(&self) -> &[T] {
177182
self
178183
}
179184
}
180185

181186
#[stable(feature = "array_borrow", since = "1.4.0")]
182187
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
188+
#[inline]
183189
fn borrow_mut(&mut self) -> &mut [T] {
184190
self
185191
}
@@ -321,6 +327,7 @@ impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
321327
type Item = &'a T;
322328
type IntoIter = Iter<'a, T>;
323329

330+
#[inline]
324331
fn into_iter(self) -> Iter<'a, T> {
325332
self.iter()
326333
}
@@ -331,6 +338,7 @@ impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] {
331338
type Item = &'a mut T;
332339
type IntoIter = IterMut<'a, T>;
333340

341+
#[inline]
334342
fn into_iter(self) -> IterMut<'a, T> {
335343
self.iter_mut()
336344
}
@@ -435,6 +443,7 @@ macro_rules! array_impl_default {
435443
{$n:expr, $t:ident $($ts:ident)*} => {
436444
#[stable(since = "1.4.0", feature = "array_default")]
437445
impl<T> Default for [T; $n] where T: Default {
446+
#[inline]
438447
fn default() -> [T; $n] {
439448
[$t::default(), $($ts::default()),*]
440449
}
@@ -444,6 +453,7 @@ macro_rules! array_impl_default {
444453
{$n:expr,} => {
445454
#[stable(since = "1.4.0", feature = "array_default")]
446455
impl<T> Default for [T; $n] {
456+
#[inline]
447457
fn default() -> [T; $n] { [] }
448458
}
449459
};
@@ -541,13 +551,15 @@ impl<T, const N: usize> [T; N] {
541551
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
542552
#[stable(feature = "array_as_slice", since = "1.57.0")]
543553
#[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")]
554+
#[inline]
544555
pub const fn as_slice(&self) -> &[T] {
545556
self
546557
}
547558

548559
/// Returns a mutable slice containing the entire array. Equivalent to
549560
/// `&mut s[..]`.
550561
#[stable(feature = "array_as_slice", since = "1.57.0")]
562+
#[inline]
551563
pub fn as_mut_slice(&mut self) -> &mut [T] {
552564
self
553565
}
@@ -783,7 +795,7 @@ where
783795
R: Try<Output = T>,
784796
R::Residual: Residual<[T; N]>,
785797
{
786-
assert!(iter.size_hint().0 >= N);
798+
#[inline]
787799
fn next<T>(mut iter: impl UncheckedIterator<Item = T>) -> impl FnMut(usize) -> T {
788800
move |_| {
789801
// SAFETY: We know that `from_fn` will call this at most N times,
@@ -792,6 +804,7 @@ where
792804
}
793805
}
794806

807+
assert!(iter.size_hint().0 >= N);
795808
try_from_fn(next(iter))
796809
}
797810

library/core/src/borrow.rs

+5
Original file line numberDiff line numberDiff line change
@@ -207,34 +207,39 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
207207
#[stable(feature = "rust1", since = "1.0.0")]
208208
impl<T: ?Sized> Borrow<T> for T {
209209
#[rustc_diagnostic_item = "noop_method_borrow"]
210+
#[inline]
210211
fn borrow(&self) -> &T {
211212
self
212213
}
213214
}
214215

215216
#[stable(feature = "rust1", since = "1.0.0")]
216217
impl<T: ?Sized> BorrowMut<T> for T {
218+
#[inline]
217219
fn borrow_mut(&mut self) -> &mut T {
218220
self
219221
}
220222
}
221223

222224
#[stable(feature = "rust1", since = "1.0.0")]
223225
impl<T: ?Sized> Borrow<T> for &T {
226+
#[inline]
224227
fn borrow(&self) -> &T {
225228
&**self
226229
}
227230
}
228231

229232
#[stable(feature = "rust1", since = "1.0.0")]
230233
impl<T: ?Sized> Borrow<T> for &mut T {
234+
#[inline]
231235
fn borrow(&self) -> &T {
232236
&**self
233237
}
234238
}
235239

236240
#[stable(feature = "rust1", since = "1.0.0")]
237241
impl<T: ?Sized> BorrowMut<T> for &mut T {
242+
#[inline]
238243
fn borrow_mut(&mut self) -> &mut T {
239244
&mut **self
240245
}

library/core/src/cell.rs

+10
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl<T: Ord + Copy> Ord for Cell<T> {
373373
#[stable(feature = "cell_from", since = "1.12.0")]
374374
impl<T> From<T> for Cell<T> {
375375
/// Creates a new `Cell<T>` containing the given value.
376+
#[inline]
376377
fn from(t: T) -> Cell<T> {
377378
Cell::new(t)
378379
}
@@ -488,6 +489,7 @@ impl<T> Cell<T> {
488489
/// ```
489490
#[stable(feature = "move_cell", since = "1.17.0")]
490491
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
492+
#[inline]
491493
pub const fn into_inner(self) -> T {
492494
self.value.into_inner()
493495
}
@@ -658,6 +660,7 @@ impl<T> Cell<[T]> {
658660
/// assert_eq!(slice_cell.len(), 3);
659661
/// ```
660662
#[stable(feature = "as_cell", since = "1.37.0")]
663+
#[inline]
661664
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
662665
// SAFETY: `Cell<T>` has the same memory layout as `T`.
663666
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
@@ -678,6 +681,7 @@ impl<T, const N: usize> Cell<[T; N]> {
678681
/// let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
679682
/// ```
680683
#[unstable(feature = "as_array_of_cells", issue = "88248")]
684+
#[inline]
681685
pub fn as_array_of_cells(&self) -> &[Cell<T>; N] {
682686
// SAFETY: `Cell<T>` has the same memory layout as `T`.
683687
unsafe { &*(self as *const Cell<[T; N]> as *const [Cell<T>; N]) }
@@ -1172,6 +1176,7 @@ impl<T: ?Sized> RefCell<T> {
11721176
/// assert!(c.try_borrow().is_ok());
11731177
/// ```
11741178
#[unstable(feature = "cell_leak", issue = "69099")]
1179+
#[inline]
11751180
pub fn undo_leak(&mut self) -> &mut T {
11761181
*self.borrow.get_mut() = UNUSED;
11771182
self.get_mut()
@@ -1356,6 +1361,7 @@ impl<T: ?Sized + Ord> Ord for RefCell<T> {
13561361
#[stable(feature = "cell_from", since = "1.12.0")]
13571362
impl<T> From<T> for RefCell<T> {
13581363
/// Creates a new `RefCell<T>` containing the given value.
1364+
#[inline]
13591365
fn from(t: T) -> RefCell<T> {
13601366
RefCell::new(t)
13611367
}
@@ -1576,6 +1582,7 @@ impl<'b, T: ?Sized> Ref<'b, T> {
15761582
/// assert!(cell.try_borrow_mut().is_err());
15771583
/// ```
15781584
#[unstable(feature = "cell_leak", issue = "69099")]
1585+
#[inline]
15791586
pub fn leak(orig: Ref<'b, T>) -> &'b T {
15801587
// By forgetting this Ref we ensure that the borrow counter in the RefCell can't go back to
15811588
// UNUSED within the lifetime `'b`. Resetting the reference tracking state would require a
@@ -1742,6 +1749,7 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
17421749
/// assert!(cell.try_borrow_mut().is_err());
17431750
/// ```
17441751
#[unstable(feature = "cell_leak", issue = "69099")]
1752+
#[inline]
17451753
pub fn leak(mut orig: RefMut<'b, T>) -> &'b mut T {
17461754
// By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell can't
17471755
// go back to UNUSED within the lifetime `'b`. Resetting the reference tracking state would
@@ -2188,6 +2196,7 @@ impl<T: Default> Default for UnsafeCell<T> {
21882196
#[stable(feature = "cell_from", since = "1.12.0")]
21892197
impl<T> From<T> for UnsafeCell<T> {
21902198
/// Creates a new `UnsafeCell<T>` containing the given value.
2199+
#[inline]
21912200
fn from(t: T) -> UnsafeCell<T> {
21922201
UnsafeCell::new(t)
21932202
}
@@ -2288,6 +2297,7 @@ impl<T: Default> Default for SyncUnsafeCell<T> {
22882297
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
22892298
impl<T> From<T> for SyncUnsafeCell<T> {
22902299
/// Creates a new `SyncUnsafeCell<T>` containing the given value.
2300+
#[inline]
22912301
fn from(t: T) -> SyncUnsafeCell<T> {
22922302
SyncUnsafeCell::new(t)
22932303
}

library/core/src/char/decode.rs

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl DecodeUtf16Error {
109109
/// Returns the unpaired surrogate which caused this error.
110110
#[must_use]
111111
#[stable(feature = "decode_utf16", since = "1.9.0")]
112+
#[inline]
112113
pub fn unpaired_surrogate(&self) -> u16 {
113114
self.code
114115
}
@@ -124,6 +125,7 @@ impl fmt::Display for DecodeUtf16Error {
124125
#[stable(feature = "decode_utf16", since = "1.9.0")]
125126
impl Error for DecodeUtf16Error {
126127
#[allow(deprecated)]
128+
#[inline]
127129
fn description(&self) -> &str {
128130
"unpaired surrogate found"
129131
}

library/core/src/char/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,19 @@ impl fmt::Display for EscapeUnicode {
219219
pub struct EscapeDefault(escape::EscapeIterInner<10>);
220220

221221
impl EscapeDefault {
222+
#[inline]
222223
fn printable(chr: ascii::Char) -> Self {
223224
let data = [chr];
224225
Self(escape::EscapeIterInner::from_array(data))
225226
}
226227

228+
#[inline]
227229
fn backslash(chr: ascii::Char) -> Self {
228230
let data = [ascii::Char::ReverseSolidus, chr];
229231
Self(escape::EscapeIterInner::from_array(data))
230232
}
231233

234+
#[inline]
232235
fn from_unicode(esc: EscapeUnicode) -> Self {
233236
Self(esc.0)
234237
}
@@ -304,20 +307,24 @@ enum EscapeDebugInner {
304307
}
305308

306309
impl EscapeDebug {
310+
#[inline]
307311
fn printable(chr: char) -> Self {
308312
Self(EscapeDebugInner::Char(chr))
309313
}
310314

315+
#[inline]
311316
fn backslash(chr: ascii::Char) -> Self {
312317
let data = [ascii::Char::ReverseSolidus, chr];
313318
let iter = escape::EscapeIterInner::from_array(data);
314319
Self(EscapeDebugInner::Bytes(iter))
315320
}
316321

322+
#[inline]
317323
fn from_unicode(esc: EscapeUnicode) -> Self {
318324
Self(EscapeDebugInner::Bytes(esc.0))
319325
}
320326

327+
#[inline]
321328
fn clear(&mut self) {
322329
let bytes = escape::EscapeIterInner::from_array([]);
323330
self.0 = EscapeDebugInner::Bytes(bytes);

0 commit comments

Comments
 (0)