Add rustfmt from relibc and apply it with cargo fmt
This commit is contained in:
+24
-10
@@ -1,8 +1,6 @@
|
||||
use core::alloc::Layout;
|
||||
use core::alloc::GlobalAlloc;
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
|
||||
use crate::common::unique::Unique;
|
||||
use crate::memory::Enomem;
|
||||
use crate::{common::unique::Unique, memory::Enomem};
|
||||
|
||||
// Necessary because GlobalAlloc::dealloc requires the layout to be the same, and therefore Box
|
||||
// cannot be used for increased alignment directly.
|
||||
@@ -24,7 +22,11 @@ impl<T: ?Sized, const ALIGN: usize> AlignedBox<T, ALIGN> {
|
||||
}
|
||||
const fn layout_upgrade_align(layout: Layout, align: usize) -> Layout {
|
||||
const fn max(a: usize, b: usize) -> usize {
|
||||
if a > b { a } else { b }
|
||||
if a > b {
|
||||
a
|
||||
} else {
|
||||
b
|
||||
}
|
||||
}
|
||||
let Ok(x) = Layout::from_size_align(layout.size(), max(align, layout.align())) else {
|
||||
panic!("failed to calculate layout");
|
||||
@@ -39,7 +41,8 @@ impl<T, const ALIGN: usize> AlignedBox<T, ALIGN> {
|
||||
T: ValidForZero,
|
||||
{
|
||||
Ok(unsafe {
|
||||
let ptr = crate::ALLOCATOR.alloc_zeroed(layout_upgrade_align(Layout::new::<T>(), ALIGN));
|
||||
let ptr =
|
||||
crate::ALLOCATOR.alloc_zeroed(layout_upgrade_align(Layout::new::<T>(), ALIGN));
|
||||
if ptr.is_null() {
|
||||
return Err(Enomem);
|
||||
}
|
||||
@@ -56,7 +59,10 @@ impl<T, const ALIGN: usize> AlignedBox<[T], ALIGN> {
|
||||
T: ValidForZero,
|
||||
{
|
||||
Ok(unsafe {
|
||||
let ptr = crate::ALLOCATOR.alloc_zeroed(layout_upgrade_align(Layout::array::<T>(len).unwrap(), ALIGN));
|
||||
let ptr = crate::ALLOCATOR.alloc_zeroed(layout_upgrade_align(
|
||||
Layout::array::<T>(len).unwrap(),
|
||||
ALIGN,
|
||||
));
|
||||
if ptr.is_null() {
|
||||
return Err(Enomem);
|
||||
}
|
||||
@@ -69,7 +75,13 @@ impl<T, const ALIGN: usize> AlignedBox<[T], ALIGN> {
|
||||
|
||||
impl<T: ?Sized, const ALIGN: usize> core::fmt::Debug for AlignedBox<T, ALIGN> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "[aligned box at {:p}, size {} alignment {}]", self.inner.as_ptr(), self.layout().size(), self.layout().align())
|
||||
write!(
|
||||
f,
|
||||
"[aligned box at {:p}, size {} alignment {}]",
|
||||
self.inner.as_ptr(),
|
||||
self.layout().size(),
|
||||
self.layout().align()
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<T: ?Sized, const ALIGN: usize> Drop for AlignedBox<T, ALIGN> {
|
||||
@@ -95,14 +107,16 @@ impl<T: ?Sized, const ALIGN: usize> core::ops::DerefMut for AlignedBox<T, ALIGN>
|
||||
}
|
||||
impl<T: Clone + ValidForZero, const ALIGN: usize> Clone for AlignedBox<T, ALIGN> {
|
||||
fn clone(&self) -> Self {
|
||||
let mut new = Self::try_zeroed().unwrap_or_else(|_| alloc::alloc::handle_alloc_error(self.layout()));
|
||||
let mut new =
|
||||
Self::try_zeroed().unwrap_or_else(|_| alloc::alloc::handle_alloc_error(self.layout()));
|
||||
T::clone_from(&mut new, self);
|
||||
new
|
||||
}
|
||||
}
|
||||
impl<T: Clone + ValidForZero, const ALIGN: usize> Clone for AlignedBox<[T], ALIGN> {
|
||||
fn clone(&self) -> Self {
|
||||
let mut new = Self::try_zeroed_slice(self.len()).unwrap_or_else(|_| alloc::alloc::handle_alloc_error(self.layout()));
|
||||
let mut new = Self::try_zeroed_slice(self.len())
|
||||
.unwrap_or_else(|_| alloc::alloc::handle_alloc_error(self.layout()));
|
||||
for i in 0..self.len() {
|
||||
new[i].clone_from(&self[i]);
|
||||
}
|
||||
|
||||
+34
-13
@@ -69,7 +69,7 @@ macro_rules! int_like {
|
||||
#[inline]
|
||||
pub const fn new(x: $new_type_name) -> Self {
|
||||
$new_atomic_type_name {
|
||||
container: $backing_atomic_type::new(x.get())
|
||||
container: $backing_atomic_type::new(x.get()),
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
@@ -84,23 +84,47 @@ macro_rules! int_like {
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
pub fn swap(&self, val: $new_type_name, order: ::core::sync::atomic::Ordering) -> $new_type_name {
|
||||
pub fn swap(
|
||||
&self,
|
||||
val: $new_type_name,
|
||||
order: ::core::sync::atomic::Ordering,
|
||||
) -> $new_type_name {
|
||||
$new_type_name::from(self.container.swap(val.into(), order))
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
pub fn compare_exchange(&self, current: $new_type_name, new: $new_type_name, success: ::core::sync::atomic::Ordering, failure: ::core::sync::atomic::Ordering) -> ::core::result::Result<$new_type_name, $new_type_name> {
|
||||
match self.container.compare_exchange(current.into(), new.into(), success, failure) {
|
||||
pub fn compare_exchange(
|
||||
&self,
|
||||
current: $new_type_name,
|
||||
new: $new_type_name,
|
||||
success: ::core::sync::atomic::Ordering,
|
||||
failure: ::core::sync::atomic::Ordering,
|
||||
) -> ::core::result::Result<$new_type_name, $new_type_name> {
|
||||
match self
|
||||
.container
|
||||
.compare_exchange(current.into(), new.into(), success, failure)
|
||||
{
|
||||
Ok(result) => Ok($new_type_name::from(result)),
|
||||
Err(result) => Err($new_type_name::from(result))
|
||||
Err(result) => Err($new_type_name::from(result)),
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
pub fn compare_exchange_weak(&self, current: $new_type_name, new: $new_type_name, success: ::core::sync::atomic::Ordering, failure: ::core::sync::atomic::Ordering) -> ::core::result::Result<$new_type_name, $new_type_name> {
|
||||
match self.container.compare_exchange_weak(current.into(), new.into(), success, failure) {
|
||||
pub fn compare_exchange_weak(
|
||||
&self,
|
||||
current: $new_type_name,
|
||||
new: $new_type_name,
|
||||
success: ::core::sync::atomic::Ordering,
|
||||
failure: ::core::sync::atomic::Ordering,
|
||||
) -> ::core::result::Result<$new_type_name, $new_type_name> {
|
||||
match self.container.compare_exchange_weak(
|
||||
current.into(),
|
||||
new.into(),
|
||||
success,
|
||||
failure,
|
||||
) {
|
||||
Ok(result) => Ok($new_type_name::from(result)),
|
||||
Err(result) => Err($new_type_name::from(result))
|
||||
Err(result) => Err($new_type_name::from(result)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,23 +134,20 @@ macro_rules! int_like {
|
||||
Self::new($new_type_name::new(0))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
use core::mem::size_of;
|
||||
use ::core::sync::atomic::AtomicUsize;
|
||||
use core::mem::size_of;
|
||||
|
||||
// Generate type `usize_like`.
|
||||
int_like!(UsizeLike, usize);
|
||||
assert_eq!(size_of::<UsizeLike>(), size_of::<usize>());
|
||||
|
||||
|
||||
// Generate types `usize_like` and `AtomicUsize`.
|
||||
int_like!(UsizeLike2, AtomicUsizeLike, usize, AtomicUsize);
|
||||
assert_eq!(size_of::<UsizeLike2>(), size_of::<usize>());
|
||||
assert_eq!(size_of::<AtomicUsizeLike>(), size_of::<AtomicUsize>());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,5 +26,3 @@ macro_rules! dbg {
|
||||
($($crate::dbg!($val)),+,)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user