Skip to content

Commit 32d3eeb

Browse files
authored
Merge pull request #169 from Rust-for-Linux/dev/inline
add `#[inline]` to small functions
2 parents df87aad + e6a27ed commit 32d3eeb

7 files changed

Lines changed: 34 additions & 0 deletions

File tree

internal/src/pin_data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ fn generate_the_pin_data(
468468
impl #impl_generics ::core::clone::Clone for __ThePinData #ty_generics
469469
#whr
470470
{
471+
#[inline]
471472
fn clone(&self) -> Self { *self }
472473
}
473474

@@ -499,6 +500,7 @@ fn generate_the_pin_data(
499500
{
500501
type PinData = __ThePinData #ty_generics;
501502

503+
#[inline]
502504
unsafe fn __pin_data() -> Self::PinData {
503505
__ThePinData { __phantom: ::pin_init::__internal::PhantomInvariant::new() }
504506
}

src/__internal.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub unsafe trait HasInitData {
105105
pub struct AllData<T: ?Sized>(PhantomInvariant<T>);
106106

107107
impl<T: ?Sized> Clone for AllData<T> {
108+
#[inline]
108109
fn clone(&self) -> Self {
109110
*self
110111
}
@@ -127,6 +128,7 @@ impl<T: ?Sized> AllData<T> {
127128
unsafe impl<T: ?Sized> HasInitData for T {
128129
type InitData = AllData<T>;
129130

131+
#[inline]
130132
unsafe fn __init_data() -> Self::InitData {
131133
AllData(PhantomInvariant::new())
132134
}
@@ -385,19 +387,22 @@ pub struct AlwaysFail<T: ?Sized> {
385387

386388
impl<T: ?Sized> AlwaysFail<T> {
387389
/// Creates a new initializer that always fails.
390+
#[inline]
388391
pub fn new() -> Self {
389392
Self { _t: PhantomData }
390393
}
391394
}
392395

393396
impl<T: ?Sized> Default for AlwaysFail<T> {
397+
#[inline]
394398
fn default() -> Self {
395399
Self::new()
396400
}
397401
}
398402

399403
// SAFETY: `__init` always fails, which is always okay.
400404
unsafe impl<T: ?Sized> PinInit<T, ()> for AlwaysFail<T> {
405+
#[inline]
401406
unsafe fn __init(self, _slot: *mut T) -> Result<(), ()> {
402407
Err(())
403408
}

src/alloc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub trait InPlaceInit<T>: Sized {
3535
/// type.
3636
///
3737
/// If `T: !Unpin` it will not be able to move afterwards.
38+
#[inline]
3839
fn pin_init(init: impl PinInit<T>) -> Result<Pin<Self>, AllocError> {
3940
// SAFETY: We delegate to `init` and only change the error type.
4041
let init = unsafe {
@@ -52,6 +53,7 @@ pub trait InPlaceInit<T>: Sized {
5253
E: From<AllocError>;
5354

5455
/// Use the given initializer to in-place initialize a `T`.
56+
#[inline]
5557
fn init(init: impl Init<T>) -> Result<Self, AllocError> {
5658
// SAFETY: We delegate to `init` and only change the error type.
5759
let init = unsafe {
@@ -136,6 +138,7 @@ impl<T> InPlaceInit<T> for Arc<T> {
136138
impl<T> InPlaceWrite<T> for Box<MaybeUninit<T>> {
137139
type Initialized = Box<T>;
138140

141+
#[inline]
139142
fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
140143
let slot = self.as_mut_ptr();
141144
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
@@ -145,6 +148,7 @@ impl<T> InPlaceWrite<T> for Box<MaybeUninit<T>> {
145148
Ok(unsafe { self.assume_init() })
146149
}
147150

151+
#[inline]
148152
fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
149153
let slot = self.as_mut_ptr();
150154
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
955955
/// Ok(())
956956
/// });
957957
/// ```
958+
#[inline]
958959
fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
959960
where
960961
F: FnOnce(Pin<&mut T>) -> Result<(), E>,
@@ -1003,6 +1004,7 @@ where
10031004
I: PinInit<T, E>,
10041005
F: FnOnce(Pin<&mut T>) -> Result<(), E>,
10051006
{
1007+
#[inline]
10061008
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
10071009
// SAFETY: All requirements fulfilled since this function is `__init`.
10081010
let slot = unsafe { __internal::Slot::<__internal::Pinned, _>::new(slot) };
@@ -1068,6 +1070,7 @@ pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
10681070
/// Ok(())
10691071
/// });
10701072
/// ```
1073+
#[inline]
10711074
fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
10721075
where
10731076
F: FnOnce(&mut T) -> Result<(), E>,
@@ -1095,6 +1098,7 @@ where
10951098
I: Init<T, E>,
10961099
F: FnOnce(&mut T) -> Result<(), E>,
10971100
{
1101+
#[inline]
10981102
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
10991103
// SAFETY: All requirements fulfilled since this function is `__init`.
11001104
let slot = unsafe { __internal::Slot::<__internal::Unpinned, _>::new(slot) };
@@ -1175,6 +1179,7 @@ pub const unsafe fn init_from_closure<T: ?Sized, E>(
11751179
///
11761180
/// - `*mut U` must be castable to `*mut T` and any value of type `T` written through such a
11771181
/// pointer must result in a valid `U`.
1182+
#[inline]
11781183
pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
11791184
// SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
11801185
// requirements.
@@ -1187,6 +1192,7 @@ pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl Pin
11871192
///
11881193
/// - `*mut U` must be castable to `*mut T` and any value of type `T` written through such a
11891194
/// pointer must result in a valid `U`.
1195+
#[inline]
11901196
pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
11911197
// SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
11921198
// requirements.
@@ -1283,6 +1289,7 @@ where
12831289
/// let array: Box<[usize; 1_000]> = Box::init(init_array_from_fn(|i| i)).unwrap();
12841290
/// assert_eq!(array.len(), 1_000);
12851291
/// ```
1292+
#[inline]
12861293
pub fn init_array_from_fn<I, const N: usize, T, E>(
12871294
make_init: impl FnMut(usize) -> I,
12881295
) -> impl Init<[T; N], E>
@@ -1307,6 +1314,7 @@ where
13071314
/// Arc::pin_init(pin_init_array_from_fn(|i| CMutex::new(i))).unwrap();
13081315
/// assert_eq!(array.len(), 1_000);
13091316
/// ```
1317+
#[inline]
13101318
pub fn pin_init_array_from_fn<I, const N: usize, T, E>(
13111319
make_init: impl FnMut(usize) -> I,
13121320
) -> impl PinInit<[T; N], E>
@@ -1342,6 +1350,7 @@ where
13421350
/// This initializer will first execute `lookup_bar()`, match on it, if it returned an error, the
13431351
/// initializer itself will fail with that error. If it returned `Ok`, then it will run the
13441352
/// initializer returned by the [`pin_init!`] invocation.
1353+
#[inline]
13451354
pub fn pin_init_scope<T, E, F, I>(make_init: F) -> impl PinInit<T, E>
13461355
where
13471356
F: FnOnce() -> Result<I, E>,
@@ -1385,6 +1394,7 @@ where
13851394
/// This initializer will first execute `lookup_bar()`, match on it, if it returned an error, the
13861395
/// initializer itself will fail with that error. If it returned `Ok`, then it will run the
13871396
/// initializer returned by the [`init!`] invocation.
1397+
#[inline]
13881398
pub fn init_scope<T, E, F, I>(make_init: F) -> impl Init<T, E>
13891399
where
13901400
F: FnOnce() -> Result<I, E>,
@@ -1409,6 +1419,7 @@ unsafe impl<T> Init<T> for T {}
14091419
// SAFETY: the `__init` function always returns `Ok(())` and initializes every field of
14101420
// `slot`. Additionally, all pinning invariants of `T` are upheld.
14111421
unsafe impl<T> PinInit<T> for T {
1422+
#[inline]
14121423
unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible> {
14131424
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
14141425
unsafe { slot.write(self) };
@@ -1423,6 +1434,7 @@ unsafe impl<T, E> Init<T, E> for Result<T, E> {}
14231434
// - `Ok(())`, `slot` was initialized and all pinned invariants of `T` are upheld.
14241435
// - `Err(err)`, slot was not written to.
14251436
unsafe impl<T, E> PinInit<T, E> for Result<T, E> {
1437+
#[inline]
14261438
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
14271439
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
14281440
unsafe { slot.write(self?) };
@@ -1449,6 +1461,7 @@ pub trait InPlaceWrite<T> {
14491461
impl<T> InPlaceWrite<T> for &'static mut MaybeUninit<T> {
14501462
type Initialized = &'static mut T;
14511463

1464+
#[inline]
14521465
fn write_init<E>(self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
14531466
let slot = self.as_mut_ptr();
14541467

@@ -1459,6 +1472,7 @@ impl<T> InPlaceWrite<T> for &'static mut MaybeUninit<T> {
14591472
unsafe { Ok(self.assume_init_mut()) }
14601473
}
14611474

1475+
#[inline]
14621476
fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
14631477
let slot = self.as_mut_ptr();
14641478

@@ -1764,13 +1778,15 @@ pub trait Wrapper<T> {
17641778
}
17651779

17661780
impl<T> Wrapper<T> for UnsafeCell<T> {
1781+
#[inline]
17671782
fn pin_init<E>(value_init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
17681783
// SAFETY: `UnsafeCell<T>` has a compatible layout to `T`.
17691784
unsafe { cast_pin_init(value_init) }
17701785
}
17711786
}
17721787

17731788
impl<T> Wrapper<T> for MaybeUninit<T> {
1789+
#[inline]
17741790
fn pin_init<E>(value_init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
17751791
// SAFETY: `MaybeUninit<T>` has a compatible layout to `T`.
17761792
unsafe { cast_pin_init(value_init) }
@@ -1779,6 +1795,7 @@ impl<T> Wrapper<T> for MaybeUninit<T> {
17791795

17801796
#[cfg(all(feature = "unsafe-pinned", CONFIG_RUSTC_HAS_UNSAFE_PINNED))]
17811797
impl<T> Wrapper<T> for core::pin::UnsafePinned<T> {
1798+
#[inline]
17821799
fn pin_init<E>(init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
17831800
// SAFETY: `UnsafePinned<T>` has a compatible layout to `T`.
17841801
unsafe { cast_pin_init(init) }

tests/ui/expand/many_generics.expanded.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const _: () = {
6262
where
6363
T: Bar<'a, 1>,
6464
{
65+
#[inline]
6566
fn clone(&self) -> Self {
6667
*self
6768
}
@@ -153,6 +154,7 @@ const _: () = {
153154
T: Bar<'a, 1>,
154155
{
155156
type PinData = __ThePinData<'a, 'b, T, SIZE>;
157+
#[inline]
156158
unsafe fn __pin_data() -> Self::PinData {
157159
__ThePinData {
158160
__phantom: ::pin_init::__internal::PhantomInvariant::new(),

tests/ui/expand/pin-data.expanded.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const _: () = {
3838
__phantom: ::pin_init::__internal::PhantomInvariant<Foo>,
3939
}
4040
impl ::core::clone::Clone for __ThePinData {
41+
#[inline]
4142
fn clone(&self) -> Self {
4243
*self
4344
}
@@ -92,6 +93,7 @@ const _: () = {
9293
}
9394
unsafe impl ::pin_init::__internal::HasPinData for Foo {
9495
type PinData = __ThePinData;
96+
#[inline]
9597
unsafe fn __pin_data() -> Self::PinData {
9698
__ThePinData {
9799
__phantom: ::pin_init::__internal::PhantomInvariant::new(),

tests/ui/expand/pinned_drop.expanded.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const _: () = {
3838
__phantom: ::pin_init::__internal::PhantomInvariant<Foo>,
3939
}
4040
impl ::core::clone::Clone for __ThePinData {
41+
#[inline]
4142
fn clone(&self) -> Self {
4243
*self
4344
}
@@ -92,6 +93,7 @@ const _: () = {
9293
}
9394
unsafe impl ::pin_init::__internal::HasPinData for Foo {
9495
type PinData = __ThePinData;
96+
#[inline]
9597
unsafe fn __pin_data() -> Self::PinData {
9698
__ThePinData {
9799
__phantom: ::pin_init::__internal::PhantomInvariant::new(),

0 commit comments

Comments
 (0)