Skip to content

Commit 22b675c

Browse files
committed
cleanup a bit, and formatting
1 parent ddefa8a commit 22b675c

File tree

8 files changed

+91
-104
lines changed

8 files changed

+91
-104
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "memapi2"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2018"
55
rust-version = "1.56.0"
66
authors = ["echohumm"]

build.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ mod checks {
100100
// from the crate
101101

102102
#[must_use]
103-
fn nonnull_slice_len<T>(ptr: NonNull<[T]>) -> usize { unsafe { (&*ptr.as_ptr()).len() } }
103+
fn nonnull_slice_len<T>(ptr: NonNull<[T]>) -> usize {
104+
unsafe { (&*ptr.as_ptr()).len() }
105+
}
104106

105107
#[must_use]
106108
fn nonnull_slice_from_raw_parts<T>(p: NonNull<T>, len: usize) -> NonNull<[T]> {

src/data/marker.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ unsafe impl UnsizedCopy for std::ffi::OsStr {}
2222
// SAFETY: `Path == OsStr == [u8]`
2323
unsafe impl UnsizedCopy for std::path::Path {}
2424

25-
// TODO: better solution than making them all unsafe
26-
2725
#[cfg(all(not(feature = "metadata"), not(feature = "sized_hierarchy")))]
2826
/// Trait indicating that a type has no metadata.
2927
///

src/data/type_props.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use {
2-
crate::{Layout, helpers::dangling_nonnull},
2+
crate::helpers::dangling_nonnull,
33
core::{
4+
alloc::Layout,
45
mem::{align_of, align_of_val, size_of, size_of_val},
56
ptr::NonNull
67
}
@@ -19,7 +20,9 @@ pub const USIZE_HIGH_BIT: usize = usize::MAX ^ (usize::MAX >> 1);
1920
/// A small helper to generate a `usize` in which only the bit at the given index is set.
2021
#[must_use]
2122
#[inline]
22-
pub const fn usize_bit(bit: u8) -> usize { USIZE_HIGH_BIT >> bit }
23+
pub const fn usize_bit(bit: u8) -> usize {
24+
USIZE_HIGH_BIT >> bit
25+
}
2326

2427
/// A trait containing constants for sized types.
2528
pub trait SizedProps: Sized {
@@ -78,7 +81,9 @@ pub trait PtrProps<T: ?Sized> {
7881
///
7982
/// References are always valid.
8083
#[inline]
81-
unsafe fn layout(&self) -> Layout { Layout::from_size_align_unchecked(self.sz(), self.aln()) }
84+
unsafe fn layout(&self) -> Layout {
85+
Layout::from_size_align_unchecked(self.sz(), self.aln())
86+
}
8287

8388
#[cfg(feature = "metadata")]
8489
/// Gets the metadata of the value.
@@ -103,7 +108,9 @@ pub trait PtrProps<T: ?Sized> {
103108
/// - aligned
104109
///
105110
/// References are always valid.
106-
unsafe fn is_zst(&self) -> bool { self.sz() == 0 }
111+
unsafe fn is_zst(&self) -> bool {
112+
self.sz() == 0
113+
}
107114

108115
/// Gets the largest safe length for a slice containing copies of `self`.
109116
///
@@ -196,10 +203,14 @@ impl_ptr_props_as_ref! {
196203

197204
impl<T: ?Sized> PtrProps<T> for NonNull<T> {
198205
#[inline]
199-
unsafe fn sz(&self) -> usize { size_of_val::<T>(&*self.as_ptr()) }
206+
unsafe fn sz(&self) -> usize {
207+
size_of_val::<T>(&*self.as_ptr())
208+
}
200209

201210
#[inline]
202-
unsafe fn aln(&self) -> usize { align_of_val::<T>(&*self.as_ptr()) }
211+
unsafe fn aln(&self) -> usize {
212+
align_of_val::<T>(&*self.as_ptr())
213+
}
203214

204215
#[cfg(feature = "metadata")]
205216
unsafe fn metadata(&self) -> <T as core::ptr::Pointee>::Metadata {

src/error.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use {
2-
crate::Layout,
3-
core::{
4-
fmt::{Debug, Display, Formatter, Result as FmtResult},
5-
ptr::NonNull
6-
}
1+
use core::{
2+
alloc::Layout,
3+
fmt::{Debug, Display, Formatter, Result as FmtResult},
4+
ptr::NonNull
75
};
86

97
/// Errors for allocator operations.

src/helpers.rs

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use {
22
crate::{
3-
Alloc, Layout,
3+
Alloc,
4+
Layout,
45
data::type_props::{
5-
PtrProps, SizedProps, USIZE_MAX_NO_HIGH_BIT, varsized_nonnull_from_raw_parts,
6-
varsized_pointer_from_raw_parts,
6+
PtrProps,
7+
SizedProps,
8+
USIZE_MAX_NO_HIGH_BIT,
9+
varsized_nonnull_from_raw_parts,
10+
varsized_pointer_from_raw_parts
711
},
8-
error::{AllocError, ArithOp, ArithOverflow, Cause, InvLayout, LayoutErr},
12+
error::{AllocError, ArithOp, ArithOverflow, Cause, InvLayout, LayoutErr}
913
},
1014
core::{
1115
mem::{forget, transmute},
1216
num::NonZeroUsize,
1317
ops::Deref,
14-
ptr::{self, NonNull},
15-
},
18+
ptr::{self, NonNull}
19+
}
1620
};
1721

1822
/// Performs a checked arithmetic operation on two `usize`s.
@@ -26,12 +30,12 @@ pub const fn checked_op(l: usize, op: ArithOp, r: usize) -> Result<usize, ArithO
2630
ArithOp::Sub => l.checked_sub(r),
2731
ArithOp::Mul => l.checked_mul(r),
2832
ArithOp::Div => l.checked_div(r),
29-
ArithOp::Rem => l.checked_rem(r),
33+
ArithOp::Rem => l.checked_rem(r)
3034
};
3135

3236
match res {
3337
Some(v) => Ok(v),
34-
None => AllocError::arith_overflow(l, op, r),
38+
None => AllocError::arith_overflow(l, op, r)
3539
}
3640
}
3741

@@ -44,7 +48,7 @@ pub const fn checked_op(l: usize, op: ArithOp, r: usize) -> Result<usize, ArithO
4448
pub fn checked_op_panic(l: usize, op: ArithOp, r: usize) -> usize {
4549
match checked_op(l, op, r) {
4650
Ok(v) => v,
47-
Err(e) => panic!("{}", e),
51+
Err(e) => panic!("{}", e)
4852
}
4953
}
5054

@@ -60,7 +64,7 @@ pub fn checked_op_panic(l: usize, op: ArithOp, r: usize) -> usize {
6064
pub const fn checked_op_panic_const(l: usize, op: ArithOp, r: usize) -> usize {
6165
match checked_op(l, op, r) {
6266
Ok(v) => v,
63-
Err(_) => panic!("An arithmetic operation overflowed"),
67+
Err(_) => panic!("An arithmetic operation overflowed")
6468
}
6569
}
6670

@@ -77,7 +81,7 @@ pub const fn checked_op_panic_const(l: usize, op: ArithOp, r: usize) -> usize {
7781
pub fn checked_op_panic_const(l: usize, op: ArithOp, r: usize) -> usize {
7882
match checked_op(l, op, r) {
7983
Ok(v) => v,
80-
Err(..) => panic!("An arithmetic operation overflowed"),
84+
Err(..) => panic!("An arithmetic operation overflowed")
8185
}
8286
}
8387

@@ -174,38 +178,6 @@ pub const fn layout_or_sz_align<T>(n: usize) -> Result<Layout, (usize, usize, La
174178
unsafe { Ok(Layout::from_size_align_unchecked(sz * n, align)) }
175179
}
176180

177-
/// Preprocesses a [`Layout`] to get its `Malloc`-compatible form (rounds the alignment up to the
178-
/// nearest multiple of [`usize::SZ`], AKA `size_of::<*const c_void>()`).
179-
///
180-
/// If the layout is already compatible, does nothing.
181-
#[cfg_attr(not(feature = "dev"), doc(hidden))]
182-
#[allow(clippy::missing_errors_doc)]
183-
pub fn preproc_layout(layout: Layout) -> Result<Layout, InvLayout> {
184-
let sz = layout.size();
185-
let align = tri!(do round_to_ptr_align(sz, layout.align()));
186-
match crate::unstable_util::lay_from_size_align(sz, align) {
187-
Ok(l) => Ok(l),
188-
Err(LayoutErr::ExceedsMax) => {
189-
AllocError::inv_layout(sz, align, LayoutErr::MallocExceedsMax)
190-
}
191-
// SAFETY: the only other error which can occur is Align, but since the alignment came from
192-
// a valid layout and round_to_ptr_align cannot make it zero or a non-power-of-two, it will
193-
// still be valid, so that error cannot occur.
194-
_ => unsafe { core::hint::unreachable_unchecked() },
195-
}
196-
}
197-
198-
pub(crate) const fn round_to_ptr_align(sz: usize, align: usize) -> Result<usize, InvLayout> {
199-
let mask = usize::SZ - 1;
200-
if align & mask == 0 {
201-
return Ok(align);
202-
}
203-
match checked_op(align, ArithOp::Add, mask) {
204-
Ok(v) => Ok(v & !mask),
205-
Err(e) => AllocError::inv_layout(sz, align, LayoutErr::MallocOverflow(e)),
206-
}
207-
}
208-
209181
const_if! {
210182
"const_extras",
211183
"Creates a `NonNull<[T]>` from a pointer and a length.\n\nThis is a helper used in place of
@@ -246,7 +218,7 @@ const_if! {
246218
#[allow(clippy::missing_errors_doc)]
247219
pub fn zsl_check<Ret, F: Fn(Layout) -> Result<Ret, AllocError>>(
248220
layout: Layout,
249-
f: F,
221+
f: F
250222
) -> Result<Ret, AllocError> {
251223
if layout.size() == 0 {
252224
Err(AllocError::ZeroSizedLayout(dangling_nonnull_for(layout)))
@@ -268,9 +240,9 @@ pub fn null_q_oserr<T>(ptr: *mut T, layout: Layout) -> Result<NonNull<u8>, Alloc
268240
#[allow(clippy::option_if_let_else)]
269241
match std::io::Error::last_os_error().raw_os_error() {
270242
Some(e) => e,
271-
None => core::hint::unreachable_unchecked(),
243+
None => core::hint::unreachable_unchecked()
272244
}
273-
}),
245+
})
274246
))
275247
} else {
276248
// SAFETY: we just checked that the pointer is non-null
@@ -319,7 +291,7 @@ pub fn null_q_dyn<T>(ptr: *mut T, layout: Layout) -> Result<NonNull<u8>, AllocEr
319291
pub fn null_q_zsl_check<T, F: Fn(Layout) -> *mut T>(
320292
layout: Layout,
321293
f: F,
322-
nq: fn(*mut T, Layout) -> Result<NonNull<u8>, AllocError>,
294+
nq: fn(*mut T, Layout) -> Result<NonNull<u8>, AllocError>
323295
) -> Result<NonNull<u8>, AllocError> {
324296
zsl_check(layout, |layout: Layout| nq(f(layout), layout))
325297
}
@@ -333,11 +305,11 @@ pub fn alloc_then<Ret, A: Alloc + ?Sized, E, F: Fn(NonNull<u8>, E) -> Ret>(
333305
a: &A,
334306
layout: Layout,
335307
e: E,
336-
then: F,
308+
then: F
337309
) -> Result<Ret, AllocError> {
338310
match a.alloc(layout) {
339311
Ok(ptr) => Ok(then(ptr, e)),
340-
Err(e) => Err(e),
312+
Err(e) => Err(e)
341313
}
342314
}
343315

@@ -373,7 +345,7 @@ pub fn alloc_then<Ret, A: Alloc + ?Sized, E, F: Fn(NonNull<u8>, E) -> Ret>(
373345
/// ```
374346
pub struct AllocGuard<'a, T: ?Sized, A: Alloc + ?Sized> {
375347
ptr: NonNull<T>,
376-
alloc: &'a A,
348+
alloc: &'a A
377349
}
378350

379351
impl<'a, T: ?Sized, A: Alloc + ?Sized> AllocGuard<'a, T, A> {
@@ -482,7 +454,7 @@ pub struct SliceAllocGuard<'a, T, A: Alloc + ?Sized> {
482454
ptr: NonNull<T>,
483455
alloc: &'a A,
484456
pub(crate) init: usize,
485-
full: usize,
457+
full: usize
486458
}
487459

488460
impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
@@ -704,7 +676,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
704676
/// `excess` if `slice.len() > remaining_capacity`.
705677
pub fn clone_from_slice(&mut self, slice: &[T]) -> Result<(), usize>
706678
where
707-
T: Clone,
679+
T: Clone
708680
{
709681
let lim = self.full - self.init;
710682
let to_clone = if slice.len() < lim { slice.len() } else { lim };
@@ -741,7 +713,7 @@ impl<'a, T, A: Alloc + ?Sized> SliceAllocGuard<'a, T, A> {
741713
ptr::write(self.ptr.as_ptr().add(self.init), elem);
742714
self.init += 1;
743715
},
744-
None => return Ok(()),
716+
None => return Ok(())
745717
}
746718
}
747719
}
@@ -755,7 +727,7 @@ impl<T, A: Alloc + ?Sized> Drop for SliceAllocGuard<'_, T, A> {
755727
ptr::drop_in_place(slice_ptr_from_raw_parts(self.ptr.as_ptr(), self.init));
756728
self.alloc.dealloc(
757729
self.ptr.cast(),
758-
Layout::from_size_align_unchecked(T::SZ * self.full, T::ALN),
730+
Layout::from_size_align_unchecked(T::SZ * self.full, T::ALN)
759731
);
760732
}
761733
}

0 commit comments

Comments
 (0)