-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit 1. makes `SizeError` struct with one generic field, instead of `enum<T> { Less(usize, T), Greater(usize, T) }` 2. removes `SizeError::{expect,expect_size}` methods 3. changes `Display` output to just "wrong size" This is done to make API clearer and remove overhead on errors caused by calculating the difference between sizes
- Loading branch information
1 parent
1e5d034
commit 387125e
Showing
4 changed files
with
58 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,11 @@ | ||
use core::{ | ||
cmp::Ordering, | ||
fmt::{Display, Error, Formatter}, | ||
}; | ||
use core::fmt::{self, Display, Formatter}; | ||
|
||
/// Error that represents difference in expected sizes of an array. | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum SizeError<T = ()> { | ||
/// Size is less than expected by `.0` | ||
Less(usize, T), | ||
/// Size is greater than expected by `.0` | ||
Greater(usize, T), | ||
} | ||
|
||
impl<T> SizeError<T> { | ||
pub(crate) fn expect(x: usize, expected: usize, data: T) -> Result<T, Self> { | ||
match x.cmp(&expected) { | ||
Ordering::Equal => Ok(data), | ||
Ordering::Less => Err(SizeError::Less(expected - x, data)), | ||
Ordering::Greater => Err(SizeError::Greater(x - expected, data)), | ||
} | ||
} | ||
|
||
pub(crate) fn expect_size<Item>(slice: &[Item], expected: usize, data: T) -> Result<T, Self> { | ||
Self::expect(slice.len(), expected, data) | ||
} | ||
} | ||
/// Error that is caused by wrong sizes of slices/arrays | ||
#[derive(Debug, PartialEq, Eq, Copy, Clone, Default)] | ||
pub struct SizeError<T = ()>(pub T); | ||
|
||
impl<T: Display> Display for SizeError<T> { | ||
#[inline] | ||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { | ||
match self { | ||
Self::Less(n, data) => write!( | ||
f, | ||
"Size is less than expected by {n}; data: {data}", | ||
n = n, | ||
data = data | ||
), | ||
Self::Greater(n, data) => write!( | ||
f, | ||
"Size is less than expected by {n}; data: {data}", | ||
n = n, | ||
data = data | ||
), | ||
} | ||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { | ||
f.write_str("wrong size") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters