Skip to content

Commit ce85c98

Browse files
committed
Auto merge of #105651 - tgross35:once-cell-inline, r=m-ou-se
Add #[inline] markers to once_cell methods Added inline markers to all simple methods under the `once_cell` feature. Relates to #74465 and #105587 This should not block #105587
2 parents bbdca4c + b9558a1 commit ce85c98

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

library/core/src/cell/lazy.rs

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
5151
///
5252
/// assert_eq!(&*lazy, "HELLO, WORLD!");
5353
/// ```
54+
#[inline]
5455
#[unstable(feature = "once_cell", issue = "74465")]
5556
pub const fn new(init: F) -> LazyCell<T, F> {
5657
LazyCell { cell: OnceCell::new(), init: Cell::new(Some(init)) }
@@ -73,6 +74,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
7374
/// assert_eq!(LazyCell::force(&lazy), &92);
7475
/// assert_eq!(&*lazy, &92);
7576
/// ```
77+
#[inline]
7678
#[unstable(feature = "once_cell", issue = "74465")]
7779
pub fn force(this: &LazyCell<T, F>) -> &T {
7880
this.cell.get_or_init(|| match this.init.take() {
@@ -85,6 +87,7 @@ impl<T, F: FnOnce() -> T> LazyCell<T, F> {
8587
#[unstable(feature = "once_cell", issue = "74465")]
8688
impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
8789
type Target = T;
90+
#[inline]
8891
fn deref(&self) -> &T {
8992
LazyCell::force(self)
9093
}
@@ -93,6 +96,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyCell<T, F> {
9396
#[unstable(feature = "once_cell", issue = "74465")]
9497
impl<T: Default> Default for LazyCell<T> {
9598
/// Creates a new lazy value using `Default` as the initializing function.
99+
#[inline]
96100
fn default() -> LazyCell<T> {
97101
LazyCell::new(T::default)
98102
}

library/core/src/cell/once.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ pub struct OnceCell<T> {
3737

3838
impl<T> OnceCell<T> {
3939
/// Creates a new empty cell.
40-
#[unstable(feature = "once_cell", issue = "74465")]
40+
#[inline]
4141
#[must_use]
42+
#[unstable(feature = "once_cell", issue = "74465")]
4243
pub const fn new() -> OnceCell<T> {
4344
OnceCell { inner: UnsafeCell::new(None) }
4445
}
4546

4647
/// Gets the reference to the underlying value.
4748
///
4849
/// Returns `None` if the cell is empty.
50+
#[inline]
4951
#[unstable(feature = "once_cell", issue = "74465")]
5052
pub fn get(&self) -> Option<&T> {
5153
// SAFETY: Safe due to `inner`'s invariant
@@ -55,6 +57,7 @@ impl<T> OnceCell<T> {
5557
/// Gets the mutable reference to the underlying value.
5658
///
5759
/// Returns `None` if the cell is empty.
60+
#[inline]
5861
#[unstable(feature = "once_cell", issue = "74465")]
5962
pub fn get_mut(&mut self) -> Option<&mut T> {
6063
self.inner.get_mut().as_mut()
@@ -82,6 +85,7 @@ impl<T> OnceCell<T> {
8285
///
8386
/// assert!(cell.get().is_some());
8487
/// ```
88+
#[inline]
8589
#[unstable(feature = "once_cell", issue = "74465")]
8690
pub fn set(&self, value: T) -> Result<(), T> {
8791
// SAFETY: Safe because we cannot have overlapping mutable borrows
@@ -123,6 +127,7 @@ impl<T> OnceCell<T> {
123127
/// let value = cell.get_or_init(|| unreachable!());
124128
/// assert_eq!(value, &92);
125129
/// ```
130+
#[inline]
126131
#[unstable(feature = "once_cell", issue = "74465")]
127132
pub fn get_or_init<F>(&self, f: F) -> &T
128133
where
@@ -205,6 +210,7 @@ impl<T> OnceCell<T> {
205210
/// cell.set("hello".to_string()).unwrap();
206211
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
207212
/// ```
213+
#[inline]
208214
#[unstable(feature = "once_cell", issue = "74465")]
209215
pub fn into_inner(self) -> Option<T> {
210216
// Because `into_inner` takes `self` by value, the compiler statically verifies
@@ -233,6 +239,7 @@ impl<T> OnceCell<T> {
233239
/// assert_eq!(cell.take(), Some("hello".to_string()));
234240
/// assert_eq!(cell.get(), None);
235241
/// ```
242+
#[inline]
236243
#[unstable(feature = "once_cell", issue = "74465")]
237244
pub fn take(&mut self) -> Option<T> {
238245
mem::take(self).into_inner()
@@ -241,6 +248,7 @@ impl<T> OnceCell<T> {
241248

242249
#[unstable(feature = "once_cell", issue = "74465")]
243250
impl<T> Default for OnceCell<T> {
251+
#[inline]
244252
fn default() -> Self {
245253
Self::new()
246254
}
@@ -258,6 +266,7 @@ impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
258266

259267
#[unstable(feature = "once_cell", issue = "74465")]
260268
impl<T: Clone> Clone for OnceCell<T> {
269+
#[inline]
261270
fn clone(&self) -> OnceCell<T> {
262271
let res = OnceCell::new();
263272
if let Some(value) = self.get() {
@@ -272,6 +281,7 @@ impl<T: Clone> Clone for OnceCell<T> {
272281

273282
#[unstable(feature = "once_cell", issue = "74465")]
274283
impl<T: PartialEq> PartialEq for OnceCell<T> {
284+
#[inline]
275285
fn eq(&self, other: &Self) -> bool {
276286
self.get() == other.get()
277287
}
@@ -283,6 +293,7 @@ impl<T: Eq> Eq for OnceCell<T> {}
283293
#[unstable(feature = "once_cell", issue = "74465")]
284294
impl<T> const From<T> for OnceCell<T> {
285295
/// Creates a new `OnceCell<T>` which already contains the given `value`.
296+
#[inline]
286297
fn from(value: T) -> Self {
287298
OnceCell { inner: UnsafeCell::new(Some(value)) }
288299
}

library/std/src/sync/lazy_lock.rs

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct LazyLock<T, F = fn() -> T> {
4949
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
5050
/// Creates a new lazy value with the given initializing
5151
/// function.
52+
#[inline]
5253
#[unstable(feature = "once_cell", issue = "74465")]
5354
pub const fn new(f: F) -> LazyLock<T, F> {
5455
LazyLock { cell: OnceLock::new(), init: Cell::new(Some(f)) }
@@ -70,6 +71,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
7071
/// assert_eq!(LazyLock::force(&lazy), &92);
7172
/// assert_eq!(&*lazy, &92);
7273
/// ```
74+
#[inline]
7375
#[unstable(feature = "once_cell", issue = "74465")]
7476
pub fn force(this: &LazyLock<T, F>) -> &T {
7577
this.cell.get_or_init(|| match this.init.take() {
@@ -82,6 +84,8 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
8284
#[unstable(feature = "once_cell", issue = "74465")]
8385
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
8486
type Target = T;
87+
88+
#[inline]
8589
fn deref(&self) -> &T {
8690
LazyLock::force(self)
8791
}
@@ -90,6 +94,7 @@ impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
9094
#[unstable(feature = "once_cell", issue = "74465")]
9195
impl<T: Default> Default for LazyLock<T> {
9296
/// Creates a new lazy value using `Default` as the initializing function.
97+
#[inline]
9398
fn default() -> LazyLock<T> {
9499
LazyLock::new(T::default)
95100
}

library/std/src/sync/once_lock.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ pub struct OnceLock<T> {
6161

6262
impl<T> OnceLock<T> {
6363
/// Creates a new empty cell.
64-
#[unstable(feature = "once_cell", issue = "74465")]
64+
#[inline]
6565
#[must_use]
66+
#[unstable(feature = "once_cell", issue = "74465")]
6667
pub const fn new() -> OnceLock<T> {
6768
OnceLock {
6869
once: Once::new(),
@@ -75,6 +76,7 @@ impl<T> OnceLock<T> {
7576
///
7677
/// Returns `None` if the cell is empty, or being initialized. This
7778
/// method never blocks.
79+
#[inline]
7880
#[unstable(feature = "once_cell", issue = "74465")]
7981
pub fn get(&self) -> Option<&T> {
8082
if self.is_initialized() {
@@ -88,6 +90,7 @@ impl<T> OnceLock<T> {
8890
/// Gets the mutable reference to the underlying value.
8991
///
9092
/// Returns `None` if the cell is empty. This method never blocks.
93+
#[inline]
9194
#[unstable(feature = "once_cell", issue = "74465")]
9295
pub fn get_mut(&mut self) -> Option<&mut T> {
9396
if self.is_initialized() {
@@ -125,6 +128,7 @@ impl<T> OnceLock<T> {
125128
/// assert_eq!(CELL.get(), Some(&92));
126129
/// }
127130
/// ```
131+
#[inline]
128132
#[unstable(feature = "once_cell", issue = "74465")]
129133
pub fn set(&self, value: T) -> Result<(), T> {
130134
let mut value = Some(value);
@@ -164,6 +168,7 @@ impl<T> OnceLock<T> {
164168
/// let value = cell.get_or_init(|| unreachable!());
165169
/// assert_eq!(value, &92);
166170
/// ```
171+
#[inline]
167172
#[unstable(feature = "once_cell", issue = "74465")]
168173
pub fn get_or_init<F>(&self, f: F) -> &T
169174
where
@@ -203,6 +208,7 @@ impl<T> OnceLock<T> {
203208
/// assert_eq!(value, Ok(&92));
204209
/// assert_eq!(cell.get(), Some(&92))
205210
/// ```
211+
#[inline]
206212
#[unstable(feature = "once_cell", issue = "74465")]
207213
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
208214
where
@@ -241,6 +247,7 @@ impl<T> OnceLock<T> {
241247
/// cell.set("hello".to_string()).unwrap();
242248
/// assert_eq!(cell.into_inner(), Some("hello".to_string()));
243249
/// ```
250+
#[inline]
244251
#[unstable(feature = "once_cell", issue = "74465")]
245252
pub fn into_inner(mut self) -> Option<T> {
246253
self.take()
@@ -267,6 +274,7 @@ impl<T> OnceLock<T> {
267274
/// assert_eq!(cell.take(), Some("hello".to_string()));
268275
/// assert_eq!(cell.get(), None);
269276
/// ```
277+
#[inline]
270278
#[unstable(feature = "once_cell", issue = "74465")]
271279
pub fn take(&mut self) -> Option<T> {
272280
if self.is_initialized() {
@@ -315,6 +323,7 @@ impl<T> OnceLock<T> {
315323
/// # Safety
316324
///
317325
/// The value must be initialized
326+
#[inline]
318327
unsafe fn get_unchecked(&self) -> &T {
319328
debug_assert!(self.is_initialized());
320329
(&*self.value.get()).assume_init_ref()
@@ -323,6 +332,7 @@ impl<T> OnceLock<T> {
323332
/// # Safety
324333
///
325334
/// The value must be initialized
335+
#[inline]
326336
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
327337
debug_assert!(self.is_initialized());
328338
(&mut *self.value.get()).assume_init_mut()
@@ -360,6 +370,7 @@ impl<T> const Default for OnceLock<T> {
360370
/// assert_eq!(OnceLock::<()>::new(), OnceLock::default());
361371
/// }
362372
/// ```
373+
#[inline]
363374
fn default() -> OnceLock<T> {
364375
OnceLock::new()
365376
}
@@ -377,6 +388,7 @@ impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
377388

378389
#[unstable(feature = "once_cell", issue = "74465")]
379390
impl<T: Clone> Clone for OnceLock<T> {
391+
#[inline]
380392
fn clone(&self) -> OnceLock<T> {
381393
let cell = Self::new();
382394
if let Some(value) = self.get() {
@@ -408,6 +420,7 @@ impl<T> From<T> for OnceLock<T> {
408420
/// Ok(())
409421
/// # }
410422
/// ```
423+
#[inline]
411424
fn from(value: T) -> Self {
412425
let cell = Self::new();
413426
match cell.set(value) {
@@ -419,6 +432,7 @@ impl<T> From<T> for OnceLock<T> {
419432

420433
#[unstable(feature = "once_cell", issue = "74465")]
421434
impl<T: PartialEq> PartialEq for OnceLock<T> {
435+
#[inline]
422436
fn eq(&self, other: &OnceLock<T>) -> bool {
423437
self.get() == other.get()
424438
}
@@ -429,6 +443,7 @@ impl<T: Eq> Eq for OnceLock<T> {}
429443

430444
#[unstable(feature = "once_cell", issue = "74465")]
431445
unsafe impl<#[may_dangle] T> Drop for OnceLock<T> {
446+
#[inline]
432447
fn drop(&mut self) {
433448
if self.is_initialized() {
434449
// SAFETY: The cell is initialized and being dropped, so it can't

0 commit comments

Comments
 (0)