Skip to content

Commit 46c9951

Browse files
committed
add Error::Unsupportd, format stuff
1 parent 8c20a56 commit 46c9951

File tree

10 files changed

+69
-60
lines changed

10 files changed

+69
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
### Added
1717

1818
- `AllocErrorType` trait shared between `alloc_mut` and `alloc` traits
19+
- `Error::Unsupported` variant for unsupported operations
1920

2021
### Changed
2122

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ std = []
5555
# allocator support
5656
## the standard c allocator
5757
c_alloc = []
58+
## c alloca-based allocator
59+
stack_alloc = ["alloc_temp_trait", "c_alloc", "cc"]
5860
## adds a trait for temporary, scoped allocations
5961
alloc_temp_trait = []
60-
stack_alloc = ["alloc_temp_trait", "c_alloc", "cc"]
6162

6263
# actual features
6364
## support best-effort os error reporting from errno

src/allocs/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ pub mod c_alloc;
55
#[cfg(feature = "stack_alloc")]
66
/// An allocator which uses C's `alloca`/`_alloca` allocation function.
77
pub mod stack_alloc;
8-
9-
// TODO: basic ptr-inc alloc with a backer + arrayalloc, rename stackalloc for clarity

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ macro_rules! impl_error {
1919
};
2020
}
2121

22+
// TODO: simplify this. it's getting a little absurd again.
23+
2224
/// Errors for allocator operations.
2325
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2426
#[repr(u8)]
@@ -45,6 +47,8 @@ pub enum Error {
4547
ArithmeticError(ArithErr),
4648
/// An unwinding panic occurred in a function which does not support unwinding; likely FFI.
4749
CaughtUnwind,
50+
/// The requested operation is unsupported.
51+
Unsupported,
4852
/// Any other kind of error, in the form of a string.
4953
Other(&'static str)
5054
}
@@ -60,6 +64,7 @@ impl Display for Error {
6064
InvalidLayout,
6165
Other,
6266
ShrinkLargerNewLayout,
67+
Unsupported,
6368
ZeroSizedLayout
6469
};
6570

@@ -90,6 +95,7 @@ impl Display for Error {
9095
CaughtUnwind => {
9196
write!(f, "unwind caught in unsupported function")
9297
}
98+
Unsupported => write!(f, "unsupported operation"),
9399
Other(other) => write!(f, "{}", other)
94100
}
95101
}

src/ffi/stack_alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use {
55
mem::{ManuallyDrop, MaybeUninit},
66
ops::FnOnce,
77
ptr::NonNull,
8-
result::Result::{self, Err, Ok},
8+
result::Result::{self, Err, Ok}
99
}
1010
};
1111

src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@
4646
//! - `full`, `full_nightly`: convenience bundles for docs/tests
4747
4848
#![allow(unknown_lints)]
49-
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::multiple_unsafe_ops_per_block)]
49+
#![deny(
50+
clippy::all,
51+
clippy::pedantic,
52+
clippy::nursery,
53+
clippy::multiple_unsafe_ops_per_block,
54+
clippy::undocumented_unsafe_blocks,
55+
missing_docs
56+
)]
57+
#![warn(clippy::missing_errors_doc)]
5058
#![allow(
5159
clippy::inline_always,
5260
clippy::borrow_as_ptr,
@@ -55,7 +63,6 @@
5563
clippy::question_mark,
5664
unused_unsafe
5765
)]
58-
#![deny(missing_docs, unused_variables, clippy::undocumented_unsafe_blocks)]
5966
#![warn(unknown_lints)]
6067
#![no_implicit_prelude]
6168
#![cfg_attr(feature = "dev", warn(rustdoc::broken_intra_doc_links))]
@@ -133,19 +140,18 @@ macro_rules! default_dealloc {
133140

134141
macro_rules! default_shrink {
135142
($self:ident::$unchecked:ident, $ptr:ident, $old:ident, $new:ident) => {
136-
match $old.size().cmp(&$new.size()) {
137-
Ordering::Less => Err(<Self as AllocError>::Error::from(Error::ShrinkLargerNewLayout(
138-
$old.size(),
139-
$new.size()
140-
))),
143+
match $new.size().cmp(&$old.size()) {
144+
Ordering::Greater => Err(<Self as AllocError>::Error::from(
145+
Error::ShrinkLargerNewLayout($old.size(), $new.size())
146+
)),
141147
Ordering::Equal => {
142148
if $new.align() > $old.align() {
143149
$unchecked($self, $ptr, $old, $new)
144150
} else {
145151
Ok($ptr)
146152
}
147153
}
148-
Ordering::Greater => $unchecked($self, $ptr, $old, $new)
154+
Ordering::Less => $unchecked($self, $ptr, $old, $new)
149155
}
150156
};
151157
}
@@ -354,5 +360,5 @@ pub(crate) mod nightly {
354360
default_alloc_impl!(Global);
355361

356362
// TODO: either Allocator for A: Alloc or vice versa, not sure which. i think i removed that at
357-
// some point but i can't remember why.
363+
// some point but i can't remember why.
358364
}

src/traits/alloc.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub trait Dealloc: Alloc + DeallocMut {
112112
/// 0</code>.
113113
/// - <code>Err([Error::DanglingDeallocation])</code> if <code>ptr ==
114114
/// [layout.dangling](Layout::dangling)</code>.
115+
/// - <code>Err([Error::Unsupported])</code> if deallocation is unsupported. In this case,
116+
/// reallocation via [`Grow`], [`Shrink`], and [`Realloc`] may still be supported.
115117
///
116118
/// However, if the `alloc_mut` feature is enabled, and using this method on a synchronization
117119
/// primitive wrapping a type which implements [`AllocMut`], an
@@ -122,6 +124,8 @@ pub trait Dealloc: Alloc + DeallocMut {
122124
ptr: NonNull<u8>,
123125
layout: Layout
124126
) -> Result<(), <Self as AllocError>::Error>;
127+
128+
// TODO: checked_dealloc that is safe, default impl returns Err(Unsupported)?
125129
}
126130

127131
/// A memory allocation interface which can also grow allocations.
@@ -377,7 +381,7 @@ macro_rules! impl_alloc_ref {
377381
fn alloc(
378382
&self,
379383
layout: Layout
380-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
384+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
381385
(**self).alloc(layout)
382386
}
383387

@@ -386,7 +390,7 @@ macro_rules! impl_alloc_ref {
386390
fn zalloc(
387391
&self,
388392
layout: Layout
389-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
393+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
390394
(**self).zalloc(layout)
391395
}
392396
}
@@ -402,7 +406,7 @@ macro_rules! impl_alloc_ref {
402406
&self,
403407
ptr: NonNull<u8>,
404408
layout: Layout
405-
) -> Result<(), <Self as AllocError>::Error> {
409+
) -> Result<(), <$t as AllocError>::Error> {
406410
(**self).try_dealloc(ptr, layout)
407411
}
408412
}
@@ -415,7 +419,7 @@ macro_rules! impl_alloc_ref {
415419
ptr: NonNull<u8>,
416420
old_layout: Layout,
417421
new_layout: Layout
418-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
422+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
419423
(**self).grow(ptr, old_layout, new_layout)
420424
}
421425

@@ -426,7 +430,7 @@ macro_rules! impl_alloc_ref {
426430
ptr: NonNull<u8>,
427431
old_layout: Layout,
428432
new_layout: Layout
429-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
433+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
430434
(**self).zgrow(ptr, old_layout, new_layout)
431435
}
432436
}
@@ -439,7 +443,7 @@ macro_rules! impl_alloc_ref {
439443
ptr: NonNull<u8>,
440444
old_layout: Layout,
441445
new_layout: Layout
442-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
446+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
443447
(**self).shrink(ptr, old_layout, new_layout)
444448
}
445449
}
@@ -452,7 +456,7 @@ macro_rules! impl_alloc_ref {
452456
ptr: NonNull<u8>,
453457
old_layout: Layout,
454458
new_layout: Layout
455-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
459+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
456460
(**self).realloc(ptr, old_layout, new_layout)
457461
}
458462

@@ -463,7 +467,7 @@ macro_rules! impl_alloc_ref {
463467
ptr: NonNull<u8>,
464468
old_layout: Layout,
465469
new_layout: Layout
466-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
470+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
467471
(**self).rezalloc(ptr, old_layout, new_layout)
468472
}
469473
}

src/traits/alloc_mut.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub trait AllocMut: AllocError {
4444
///
4545
/// [last_os_error]: ::std::io::Error::last_os_error
4646
/// [raw_os_error]: ::std::io::Error::raw_os_error
47-
fn alloc_mut(&mut self, layout: Layout)
48-
-> Result<NonNull<u8>, <Self as AllocError>::Error>;
47+
fn alloc_mut(&mut self, layout: Layout) -> Result<NonNull<u8>, <Self as AllocError>::Error>;
4948

5049
/// Attempts to allocate a zeroed block of memory fitting the given [`Layout`].
5150
///
@@ -66,10 +65,7 @@ pub trait AllocMut: AllocError {
6665
/// [raw_os_error]: ::std::io::Error::raw_os_error
6766
#[cfg_attr(miri, track_caller)]
6867
#[inline]
69-
fn zalloc_mut(
70-
&mut self,
71-
layout: Layout
72-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
68+
fn zalloc_mut(&mut self, layout: Layout) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
7369
zalloc!(self, alloc_mut, layout)
7470
}
7571
}
@@ -125,6 +121,8 @@ pub trait DeallocMut: AllocMut {
125121
/// 0</code>.
126122
/// - <code>Err([Error::DanglingDeallocation])</code> if <code>ptr ==
127123
/// [layout.dangling](Layout::dangling)</code>.
124+
/// - <code>Err([Error::Unsupported]))</code> if deallocation is unsupported. In this case,
125+
/// reallocation via [`Grow`], [`Shrink`], and [`Realloc`] may still be supported.
128126
///
129127
/// However, if using this method through a synchronization primitive wrapping a type which
130128
/// implements [`DeallocMut`], an [`Error::Other`] wrapping a generic error message will be
@@ -392,19 +390,13 @@ impl<A: ReallocMut + GrowMut + ShrinkMut + AllocMut + DeallocMut> FullAllocMut f
392390
impl<A: Alloc + ?Sized> AllocMut for A {
393391
#[cfg_attr(miri, track_caller)]
394392
#[inline(always)]
395-
fn alloc_mut(
396-
&mut self,
397-
layout: Layout
398-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
393+
fn alloc_mut(&mut self, layout: Layout) -> Result<NonNull<u8>, <A as AllocError>::Error> {
399394
(*self).alloc(layout)
400395
}
401396

402397
#[cfg_attr(miri, track_caller)]
403398
#[inline(always)]
404-
fn zalloc_mut(
405-
&mut self,
406-
layout: Layout
407-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
399+
fn zalloc_mut(&mut self, layout: Layout) -> Result<NonNull<u8>, <A as AllocError>::Error> {
408400
(*self).zalloc(layout)
409401
}
410402
}
@@ -422,7 +414,7 @@ impl<A: Dealloc + ?Sized> DeallocMut for A {
422414
&mut self,
423415
ptr: NonNull<u8>,
424416
layout: Layout
425-
) -> Result<(), <Self as AllocError>::Error> {
417+
) -> Result<(), <A as AllocError>::Error> {
426418
(*self).try_dealloc(ptr, layout)
427419
}
428420
}
@@ -435,7 +427,7 @@ impl<A: Grow + ?Sized> GrowMut for A {
435427
ptr: NonNull<u8>,
436428
old_layout: Layout,
437429
new_layout: Layout
438-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
430+
) -> Result<NonNull<u8>, <A as AllocError>::Error> {
439431
(*self).grow(ptr, old_layout, new_layout)
440432
}
441433

@@ -446,7 +438,7 @@ impl<A: Grow + ?Sized> GrowMut for A {
446438
ptr: NonNull<u8>,
447439
old_layout: Layout,
448440
new_layout: Layout
449-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
441+
) -> Result<NonNull<u8>, <A as AllocError>::Error> {
450442
(*self).zgrow(ptr, old_layout, new_layout)
451443
}
452444
}
@@ -459,7 +451,7 @@ impl<A: Shrink + ?Sized> ShrinkMut for A {
459451
ptr: NonNull<u8>,
460452
old_layout: Layout,
461453
new_layout: Layout
462-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
454+
) -> Result<NonNull<u8>, <A as AllocError>::Error> {
463455
(*self).shrink(ptr, old_layout, new_layout)
464456
}
465457
}
@@ -472,7 +464,7 @@ impl<A: Realloc + ?Sized> ReallocMut for A {
472464
ptr: NonNull<u8>,
473465
old_layout: Layout,
474466
new_layout: Layout
475-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
467+
) -> Result<NonNull<u8>, <A as AllocError>::Error> {
476468
(*self).realloc(ptr, old_layout, new_layout)
477469
}
478470

@@ -483,7 +475,7 @@ impl<A: Realloc + ?Sized> ReallocMut for A {
483475
ptr: NonNull<u8>,
484476
old_layout: Layout,
485477
new_layout: Layout
486-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
478+
) -> Result<NonNull<u8>, <A as AllocError>::Error> {
487479
(*self).rezalloc(ptr, old_layout, new_layout)
488480
}
489481
}
@@ -503,7 +495,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
503495
fn alloc(
504496
&self,
505497
layout: Layout
506-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
498+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
507499
tri!(
508500
cmap(
509501
Error::Other(concat!(
@@ -522,7 +514,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
522514
fn zalloc(
523515
&self,
524516
layout: Layout
525-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
517+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
526518
tri!(
527519
cmap(
528520
Error::Other(concat!(
@@ -561,7 +553,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
561553
&self,
562554
ptr: NonNull<u8>,
563555
layout: Layout
564-
) -> Result<(), <Self as AllocError>::Error> {
556+
) -> Result<(), <$t as AllocError>::Error> {
565557
tri!(
566558
cmap(
567559
Error::Other(concat!(
@@ -585,7 +577,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
585577
ptr: NonNull<u8>,
586578
old_layout: Layout,
587579
new_layout: Layout,
588-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
580+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
589581
tri!(
590582
cmap(
591583
Error::Other(concat!(
@@ -607,7 +599,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
607599
ptr: NonNull<u8>,
608600
old_layout: Layout,
609601
new_layout: Layout,
610-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
602+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
611603
tri!(
612604
cmap(
613605
Error::Other(concat!(
@@ -631,7 +623,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
631623
ptr: NonNull<u8>,
632624
old_layout: Layout,
633625
new_layout: Layout,
634-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
626+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
635627
tri!(
636628
cmap(
637629
Error::Other(concat!(
@@ -655,7 +647,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
655647
ptr: NonNull<u8>,
656648
old_layout: Layout,
657649
new_layout: Layout,
658-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
650+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
659651
tri!(
660652
cmap(
661653
Error::Other(concat!(
@@ -677,7 +669,7 @@ macro_rules! impl_alloc_for_sync_mutalloc {
677669
ptr: NonNull<u8>,
678670
old_layout: Layout,
679671
new_layout: Layout,
680-
) -> Result<NonNull<u8>, <Self as AllocError>::Error> {
672+
) -> Result<NonNull<u8>, <$t as AllocError>::Error> {
681673
tri!(
682674
cmap(
683675
Error::Other(concat!(

0 commit comments

Comments
 (0)