Skip to content

Commit dc77d49

Browse files
committed
Make a bunch of trivial methods of NonNull be #[inline]
1 parent cae6efc commit dc77d49

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libcore/ptr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2867,6 +2867,7 @@ impl<T: Sized> NonNull<T> {
28672867
/// sentinel value. Types that lazily allocate must track initialization by
28682868
/// some other means.
28692869
#[stable(feature = "nonnull", since = "1.25.0")]
2870+
#[inline]
28702871
pub fn dangling() -> Self {
28712872
unsafe {
28722873
let ptr = mem::align_of::<T>() as *mut T;
@@ -2882,12 +2883,14 @@ impl<T: ?Sized> NonNull<T> {
28822883
///
28832884
/// `ptr` must be non-null.
28842885
#[stable(feature = "nonnull", since = "1.25.0")]
2886+
#[inline]
28852887
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
28862888
NonNull { pointer: NonZero(ptr as _) }
28872889
}
28882890

28892891
/// Creates a new `NonNull` if `ptr` is non-null.
28902892
#[stable(feature = "nonnull", since = "1.25.0")]
2893+
#[inline]
28912894
pub fn new(ptr: *mut T) -> Option<Self> {
28922895
if !ptr.is_null() {
28932896
Some(NonNull { pointer: NonZero(ptr as _) })
@@ -2898,6 +2901,7 @@ impl<T: ?Sized> NonNull<T> {
28982901

28992902
/// Acquires the underlying `*mut` pointer.
29002903
#[stable(feature = "nonnull", since = "1.25.0")]
2904+
#[inline]
29012905
pub fn as_ptr(self) -> *mut T {
29022906
self.pointer.0 as *mut T
29032907
}
@@ -2908,6 +2912,7 @@ impl<T: ?Sized> NonNull<T> {
29082912
/// it were actually an instance of T that is getting borrowed. If a longer
29092913
/// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
29102914
#[stable(feature = "nonnull", since = "1.25.0")]
2915+
#[inline]
29112916
pub unsafe fn as_ref(&self) -> &T {
29122917
&*self.as_ptr()
29132918
}
@@ -2918,12 +2923,14 @@ impl<T: ?Sized> NonNull<T> {
29182923
/// it were actually an instance of T that is getting borrowed. If a longer
29192924
/// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
29202925
#[stable(feature = "nonnull", since = "1.25.0")]
2926+
#[inline]
29212927
pub unsafe fn as_mut(&mut self) -> &mut T {
29222928
&mut *self.as_ptr()
29232929
}
29242930

29252931
/// Cast to a pointer of another type
29262932
#[stable(feature = "nonnull_cast", since = "1.27.0")]
2933+
#[inline]
29272934
pub fn cast<U>(self) -> NonNull<U> {
29282935
unsafe {
29292936
NonNull::new_unchecked(self.as_ptr() as *mut U)
@@ -2963,48 +2970,55 @@ impl<T: ?Sized> Eq for NonNull<T> {}
29632970

29642971
#[stable(feature = "nonnull", since = "1.25.0")]
29652972
impl<T: ?Sized> PartialEq for NonNull<T> {
2973+
#[inline]
29662974
fn eq(&self, other: &Self) -> bool {
29672975
self.as_ptr() == other.as_ptr()
29682976
}
29692977
}
29702978

29712979
#[stable(feature = "nonnull", since = "1.25.0")]
29722980
impl<T: ?Sized> Ord for NonNull<T> {
2981+
#[inline]
29732982
fn cmp(&self, other: &Self) -> Ordering {
29742983
self.as_ptr().cmp(&other.as_ptr())
29752984
}
29762985
}
29772986

29782987
#[stable(feature = "nonnull", since = "1.25.0")]
29792988
impl<T: ?Sized> PartialOrd for NonNull<T> {
2989+
#[inline]
29802990
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
29812991
self.as_ptr().partial_cmp(&other.as_ptr())
29822992
}
29832993
}
29842994

29852995
#[stable(feature = "nonnull", since = "1.25.0")]
29862996
impl<T: ?Sized> hash::Hash for NonNull<T> {
2997+
#[inline]
29872998
fn hash<H: hash::Hasher>(&self, state: &mut H) {
29882999
self.as_ptr().hash(state)
29893000
}
29903001
}
29913002

29923003
#[unstable(feature = "ptr_internals", issue = "0")]
29933004
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
3005+
#[inline]
29943006
fn from(unique: Unique<T>) -> Self {
29953007
NonNull { pointer: unique.pointer }
29963008
}
29973009
}
29983010

29993011
#[stable(feature = "nonnull", since = "1.25.0")]
30003012
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
3013+
#[inline]
30013014
fn from(reference: &'a mut T) -> Self {
30023015
NonNull { pointer: NonZero(reference as _) }
30033016
}
30043017
}
30053018

30063019
#[stable(feature = "nonnull", since = "1.25.0")]
30073020
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
3021+
#[inline]
30083022
fn from(reference: &'a T) -> Self {
30093023
NonNull { pointer: NonZero(reference as _) }
30103024
}

0 commit comments

Comments
 (0)