diff options
| author | David Tolnay <dtolnay@gmail.com> | 2019-12-06 20:18:12 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2019-12-06 20:20:51 -0800 |
| commit | c737169fe579327bd42bf04134a375ae8d8d0d7b (patch) | |
| tree | ebd04655859932a3a68029ee409a0f087c69b156 /src/libcore/ptr | |
| parent | 24d7f72c6291030d5a3906c74f9a40d1af2944b0 (diff) | |
Format libcore with rustfmt (including tests and benches)
Diffstat (limited to 'src/libcore/ptr')
| -rw-r--r-- | src/libcore/ptr/mod.rs | 234 |
1 files changed, 143 insertions, 91 deletions
diff --git a/src/libcore/ptr/mod.rs b/src/libcore/ptr/mod.rs index a8cc440c239..24ffa348329 100644 --- a/src/libcore/ptr/mod.rs +++ b/src/libcore/ptr/mod.rs @@ -70,11 +70,11 @@ #![stable(feature = "rust1", since = "1.0.0")] -use crate::intrinsics; +use crate::cmp::Ordering::{self, Equal, Greater, Less}; use crate::fmt; use crate::hash; +use crate::intrinsics; use crate::mem::{self, MaybeUninit}; -use crate::cmp::Ordering::{self, Less, Equal, Greater}; #[stable(feature = "rust1", since = "1.0.0")] pub use crate::intrinsics::copy_nonoverlapping; @@ -198,7 +198,9 @@ unsafe fn real_drop_in_place<T: ?Sized>(to_drop: &mut T) { #[inline(always)] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] -pub const fn null<T>() -> *const T { 0 as *const T } +pub const fn null<T>() -> *const T { + 0 as *const T +} /// Creates a null mutable raw pointer. /// @@ -213,7 +215,9 @@ pub const fn null<T>() -> *const T { 0 as *const T } #[inline(always)] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] -pub const fn null_mut<T>() -> *mut T { 0 as *mut T } +pub const fn null_mut<T>() -> *mut T { + 0 as *mut T +} #[repr(C)] pub(crate) union Repr<T> { @@ -705,9 +709,7 @@ pub unsafe fn read<T>(src: *const T) -> T { #[stable(feature = "ptr_unaligned", since = "1.17.0")] pub unsafe fn read_unaligned<T>(src: *const T) -> T { let mut tmp = MaybeUninit::<T>::uninit(); - copy_nonoverlapping(src as *const u8, - tmp.as_mut_ptr() as *mut u8, - mem::size_of::<T>()); + copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, mem::size_of::<T>()); tmp.assume_init() } @@ -890,9 +892,7 @@ pub unsafe fn write<T>(dst: *mut T, src: T) { #[inline] #[stable(feature = "ptr_unaligned", since = "1.17.0")] pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) { - copy_nonoverlapping(&src as *const T as *const u8, - dst as *mut u8, - mem::size_of::<T>()); + copy_nonoverlapping(&src as *const T as *const u8, dst as *mut u8, mem::size_of::<T>()); mem::forget(src); } @@ -1123,11 +1123,7 @@ impl<T: ?Sized> *const T { #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] pub unsafe fn as_ref<'a>(self) -> Option<&'a T> { - if self.is_null() { - None - } else { - Some(&*self) - } + if self.is_null() { None } else { Some(&*self) } } /// Calculates the offset from a pointer. @@ -1183,7 +1179,10 @@ impl<T: ?Sized> *const T { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub unsafe fn offset(self, count: isize) -> *const T where T: Sized { + pub unsafe fn offset(self, count: isize) -> *const T + where + T: Sized, + { intrinsics::offset(self, count) } @@ -1238,10 +1237,11 @@ impl<T: ?Sized> *const T { /// ``` #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")] #[inline] - pub fn wrapping_offset(self, count: isize) -> *const T where T: Sized { - unsafe { - intrinsics::arith_offset(self, count) - } + pub fn wrapping_offset(self, count: isize) -> *const T + where + T: Sized, + { + unsafe { intrinsics::arith_offset(self, count) } } /// Calculates the distance between two pointers. The returned value is in @@ -1309,7 +1309,10 @@ impl<T: ?Sized> *const T { #[unstable(feature = "ptr_offset_from", issue = "41079")] #[rustc_const_unstable(feature = "const_ptr_offset_from")] #[inline] - pub const unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized { + pub const unsafe fn offset_from(self, origin: *const T) -> isize + where + T: Sized, + { let pointee_size = mem::size_of::<T>(); let ok = 0 < pointee_size && pointee_size <= isize::max_value() as usize; // assert that the pointee size is valid in a const eval compatible way @@ -1354,7 +1357,10 @@ impl<T: ?Sized> *const T { /// ``` #[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")] #[inline] - pub fn wrapping_offset_from(self, origin: *const T) -> isize where T: Sized { + pub fn wrapping_offset_from(self, origin: *const T) -> isize + where + T: Sized, + { let pointee_size = mem::size_of::<T>(); assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize); @@ -1416,7 +1422,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn add(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.offset(count as isize) } @@ -1476,7 +1483,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn sub(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.offset((count as isize).wrapping_neg()) } @@ -1530,7 +1538,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub fn wrapping_add(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.wrapping_offset(count as isize) } @@ -1584,7 +1593,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub fn wrapping_sub(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.wrapping_offset((count as isize).wrapping_neg()) } @@ -1598,7 +1608,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn read(self) -> T - where T: Sized, + where + T: Sized, { read(self) } @@ -1616,7 +1627,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn read_volatile(self) -> T - where T: Sized, + where + T: Sized, { read_volatile(self) } @@ -1632,7 +1644,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn read_unaligned(self) -> T - where T: Sized, + where + T: Sized, { read_unaligned(self) } @@ -1648,7 +1661,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn copy_to(self, dest: *mut T, count: usize) - where T: Sized, + where + T: Sized, { copy(self, dest, count) } @@ -1664,7 +1678,8 @@ impl<T: ?Sized> *const T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize) - where T: Sized, + where + T: Sized, { copy_nonoverlapping(self, dest, count) } @@ -1709,17 +1724,17 @@ impl<T: ?Sized> *const T { /// # } } /// ``` #[stable(feature = "align_offset", since = "1.36.0")] - pub fn align_offset(self, align: usize) -> usize where T: Sized { + pub fn align_offset(self, align: usize) -> usize + where + T: Sized, + { if !align.is_power_of_two() { panic!("align_offset: align is not a power-of-two"); } - unsafe { - align_offset(self, align) - } + unsafe { align_offset(self, align) } } } - #[lang = "mut_ptr"] impl<T: ?Sized> *mut T { /// Returns `true` if the pointer is null. @@ -1806,11 +1821,7 @@ impl<T: ?Sized> *mut T { #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] pub unsafe fn as_ref<'a>(self) -> Option<&'a T> { - if self.is_null() { - None - } else { - Some(&*self) - } + if self.is_null() { None } else { Some(&*self) } } /// Calculates the offset from a pointer. @@ -1866,7 +1877,10 @@ impl<T: ?Sized> *mut T { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub unsafe fn offset(self, count: isize) -> *mut T where T: Sized { + pub unsafe fn offset(self, count: isize) -> *mut T + where + T: Sized, + { intrinsics::offset(self, count) as *mut T } @@ -1920,10 +1934,11 @@ impl<T: ?Sized> *mut T { /// ``` #[stable(feature = "ptr_wrapping_offset", since = "1.16.0")] #[inline] - pub fn wrapping_offset(self, count: isize) -> *mut T where T: Sized { - unsafe { - intrinsics::arith_offset(self, count) as *mut T - } + pub fn wrapping_offset(self, count: isize) -> *mut T + where + T: Sized, + { + unsafe { intrinsics::arith_offset(self, count) as *mut T } } /// Returns `None` if the pointer is null, or else returns a mutable @@ -1968,11 +1983,7 @@ impl<T: ?Sized> *mut T { #[stable(feature = "ptr_as_ref", since = "1.9.0")] #[inline] pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> { - if self.is_null() { - None - } else { - Some(&mut *self) - } + if self.is_null() { None } else { Some(&mut *self) } } /// Calculates the distance between two pointers. The returned value is in @@ -2040,7 +2051,10 @@ impl<T: ?Sized> *mut T { #[unstable(feature = "ptr_offset_from", issue = "41079")] #[rustc_const_unstable(feature = "const_ptr_offset_from")] #[inline] - pub const unsafe fn offset_from(self, origin: *const T) -> isize where T: Sized { + pub const unsafe fn offset_from(self, origin: *const T) -> isize + where + T: Sized, + { (self as *const T).offset_from(origin) } @@ -2080,7 +2094,10 @@ impl<T: ?Sized> *mut T { /// ``` #[unstable(feature = "ptr_wrapping_offset_from", issue = "41079")] #[inline] - pub fn wrapping_offset_from(self, origin: *const T) -> isize where T: Sized { + pub fn wrapping_offset_from(self, origin: *const T) -> isize + where + T: Sized, + { (self as *const T).wrapping_offset_from(origin) } @@ -2138,7 +2155,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn add(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.offset(count as isize) } @@ -2198,7 +2216,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn sub(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.offset((count as isize).wrapping_neg()) } @@ -2252,7 +2271,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub fn wrapping_add(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.wrapping_offset(count as isize) } @@ -2306,7 +2326,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub fn wrapping_sub(self, count: usize) -> Self - where T: Sized, + where + T: Sized, { self.wrapping_offset((count as isize).wrapping_neg()) } @@ -2320,7 +2341,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn read(self) -> T - where T: Sized, + where + T: Sized, { read(self) } @@ -2338,7 +2360,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn read_volatile(self) -> T - where T: Sized, + where + T: Sized, { read_volatile(self) } @@ -2354,7 +2377,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn read_unaligned(self) -> T - where T: Sized, + where + T: Sized, { read_unaligned(self) } @@ -2370,7 +2394,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn copy_to(self, dest: *mut T, count: usize) - where T: Sized, + where + T: Sized, { copy(self, dest, count) } @@ -2386,7 +2411,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize) - where T: Sized, + where + T: Sized, { copy_nonoverlapping(self, dest, count) } @@ -2402,7 +2428,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn copy_from(self, src: *const T, count: usize) - where T: Sized, + where + T: Sized, { copy(src, self, count) } @@ -2418,7 +2445,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn copy_from_nonoverlapping(self, src: *const T, count: usize) - where T: Sized, + where + T: Sized, { copy_nonoverlapping(src, self, count) } @@ -2443,7 +2471,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn write(self, val: T) - where T: Sized, + where + T: Sized, { write(self, val) } @@ -2457,7 +2486,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn write_bytes(self, val: u8, count: usize) - where T: Sized, + where + T: Sized, { write_bytes(self, val, count) } @@ -2475,7 +2505,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn write_volatile(self, val: T) - where T: Sized, + where + T: Sized, { write_volatile(self, val) } @@ -2491,7 +2522,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn write_unaligned(self, val: T) - where T: Sized, + where + T: Sized, { write_unaligned(self, val) } @@ -2505,7 +2537,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn replace(self, src: T) -> T - where T: Sized, + where + T: Sized, { replace(self, src) } @@ -2520,7 +2553,8 @@ impl<T: ?Sized> *mut T { #[stable(feature = "pointer_methods", since = "1.26.0")] #[inline] pub unsafe fn swap(self, with: *mut T) - where T: Sized, + where + T: Sized, { swap(self, with) } @@ -2565,13 +2599,14 @@ impl<T: ?Sized> *mut T { /// # } } /// ``` #[stable(feature = "align_offset", since = "1.36.0")] - pub fn align_offset(self, align: usize) -> usize where T: Sized { + pub fn align_offset(self, align: usize) -> usize + where + T: Sized, + { if !align.is_power_of_two() { panic!("align_offset: align is not a power-of-two"); } - unsafe { - align_offset(self, align) - } + unsafe { align_offset(self, align) } } } @@ -2589,7 +2624,7 @@ impl<T: ?Sized> *mut T { /// than trying to adapt this to accommodate that change. /// /// Any questions go to @nagisa. -#[lang="align_offset"] +#[lang = "align_offset"] pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize { /// Calculate multiplicative modular inverse of `x` modulo `m`. /// @@ -2629,9 +2664,8 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize { // uses e.g., subtraction `mod n`. It is entirely fine to do them `mod // usize::max_value()` instead, because we take the result `mod n` at the end // anyway. - inverse = inverse.wrapping_mul( - 2usize.wrapping_sub(x.wrapping_mul(inverse)) - ) & (going_mod - 1); + inverse = inverse.wrapping_mul(2usize.wrapping_sub(x.wrapping_mul(inverse))) + & (going_mod - 1); if going_mod > m { return inverse & (m - 1); } @@ -2690,13 +2724,13 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize { usize::max_value() } - - // Equality for pointers #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> PartialEq for *const T { #[inline] - fn eq(&self, other: &*const T) -> bool { *self == *other } + fn eq(&self, other: &*const T) -> bool { + *self == *other + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -2705,7 +2739,9 @@ impl<T: ?Sized> Eq for *const T {} #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> PartialEq for *mut T { #[inline] - fn eq(&self, other: &*mut T) -> bool { *self == *other } + fn eq(&self, other: &*mut T) -> bool { + *self == *other + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -2891,7 +2927,7 @@ macro_rules! fnptr_impls_args { }; } -fnptr_impls_args! { } +fnptr_impls_args! {} fnptr_impls_args! { A } fnptr_impls_args! { A, B } fnptr_impls_args! { A, B, C } @@ -2928,16 +2964,24 @@ impl<T: ?Sized> PartialOrd for *const T { } #[inline] - fn lt(&self, other: &*const T) -> bool { *self < *other } + fn lt(&self, other: &*const T) -> bool { + *self < *other + } #[inline] - fn le(&self, other: &*const T) -> bool { *self <= *other } + fn le(&self, other: &*const T) -> bool { + *self <= *other + } #[inline] - fn gt(&self, other: &*const T) -> bool { *self > *other } + fn gt(&self, other: &*const T) -> bool { + *self > *other + } #[inline] - fn ge(&self, other: &*const T) -> bool { *self >= *other } + fn ge(&self, other: &*const T) -> bool { + *self >= *other + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -2962,14 +3006,22 @@ impl<T: ?Sized> PartialOrd for *mut T { } #[inline] - fn lt(&self, other: &*mut T) -> bool { *self < *other } + fn lt(&self, other: &*mut T) -> bool { + *self < *other + } #[inline] - fn le(&self, other: &*mut T) -> bool { *self <= *other } + fn le(&self, other: &*mut T) -> bool { + *self <= *other + } #[inline] - fn gt(&self, other: &*mut T) -> bool { *self > *other } + fn gt(&self, other: &*mut T) -> bool { + *self > *other + } #[inline] - fn ge(&self, other: &*mut T) -> bool { *self >= *other } + fn ge(&self, other: &*mut T) -> bool { + *self >= *other + } } |
