Skip to content

Commit 5f60fc7

Browse files
authored
Merge pull request #3 from zeenix/error-type
🥅 Add Error type
2 parents 7b9f5b4 + e88bd63 commit 5f60fc7

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

src/error.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// The `Error` type for the `mayheap` crate.
2+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3+
pub enum Error {
4+
/// Attempted to grow a collection beyond its capacity.
5+
///
6+
/// This error can only occur when `heapless` feature is enabled.
7+
BufferOverflow,
8+
/// Invalid UTF-8 sequence.
9+
Utf8Error(core::str::Utf8Error),
10+
}
11+
12+
/// The Result type for the zlink crate.
13+
pub type Result<T> = core::result::Result<T, Error>;
14+
15+
impl core::error::Error for Error {
16+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
17+
match self {
18+
Error::BufferOverflow => None,
19+
Error::Utf8Error(err) => Some(err),
20+
}
21+
}
22+
}
23+
24+
impl core::fmt::Display for Error {
25+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26+
match self {
27+
Error::BufferOverflow => {
28+
write!(f, "Attempted to grow a collection beyond its capacity")
29+
}
30+
Error::Utf8Error(err) => {
31+
write!(f, "Invalid UTF-8 sequence: {}", err)
32+
}
33+
}
34+
}
35+
}
36+
37+
impl From<core::str::Utf8Error> for Error {
38+
fn from(err: core::str::Utf8Error) -> Self {
39+
Error::Utf8Error(err)
40+
}
41+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ pub use vec::Vec;
2121
pub mod string;
2222
pub use string::String;
2323

24+
mod error;
25+
pub use error::{Error, Result};
26+
2427
#[cfg(test)]
2528
mod tests {
2629
#[cfg(feature = "serde")]

src/string.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
//! A UTF-8–encoded, growable string.
22
3-
use core::{
4-
cmp::Ordering,
5-
fmt, hash, iter, ops,
6-
str::{self, Utf8Error},
7-
};
3+
use core::{cmp::Ordering, fmt, hash, iter, ops, str};
84

95
use crate::Vec;
106

@@ -42,15 +38,15 @@ impl<const N: usize> String<N> {
4238

4339
/// Convert UTF-8 bytes into a `String`.
4440
#[inline]
45-
pub fn from_utf8(vec: Vec<u8, N>) -> Result<Self, Utf8Error> {
41+
pub fn from_utf8(vec: Vec<u8, N>) -> crate::Result<Self> {
4642
let res = Inner::from_utf8(vec.into_inner()).map(Self);
4743
#[cfg(feature = "alloc")]
4844
{
49-
res.map_err(|e| e.utf8_error())
45+
res.map_err(|e| e.utf8_error().into())
5046
}
5147
#[cfg(not(feature = "alloc"))]
5248
{
53-
res
49+
res.map_err(|e| e.into())
5450
}
5551
}
5652

@@ -100,15 +96,17 @@ impl<const N: usize> String<N> {
10096

10197
/// Appends a given string slice onto the end of this `String`.
10298
#[inline]
103-
pub fn push_str(&mut self, string: &str) -> Result<(), ()> {
99+
pub fn push_str(&mut self, string: &str) -> crate::Result<()> {
104100
#[cfg(feature = "alloc")]
105101
{
106102
self.0.push_str(string);
107103
Ok(())
108104
}
109105
#[cfg(not(feature = "alloc"))]
110106
{
111-
self.0.push_str(string)
107+
self.0
108+
.push_str(string)
109+
.map_err(|_| crate::Error::BufferOverflow)
112110
}
113111
}
114112

@@ -122,15 +120,15 @@ impl<const N: usize> String<N> {
122120

123121
/// Appends the given [`char`] to the end of this `String`.
124122
#[inline]
125-
pub fn push(&mut self, c: char) -> Result<(), ()> {
123+
pub fn push(&mut self, c: char) -> crate::Result<()> {
126124
#[cfg(feature = "alloc")]
127125
{
128126
self.0.push(c);
129127
Ok(())
130128
}
131129
#[cfg(not(feature = "alloc"))]
132130
{
133-
self.0.push(c)
131+
self.0.push(c).map_err(|_| crate::Error::BufferOverflow)
134132
}
135133
}
136134

@@ -180,19 +178,21 @@ impl<const N: usize> Default for String<N> {
180178
}
181179

182180
impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
183-
type Error = ();
181+
type Error = crate::Error;
184182
#[inline]
185183
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
186-
<Self as core::str::FromStr>::from_str(s)
184+
<Self as core::str::FromStr>::from_str(s).map_err(|_| crate::Error::BufferOverflow)
187185
}
188186
}
189187

190188
impl<const N: usize> str::FromStr for String<N> {
191-
type Err = ();
189+
type Err = crate::Error;
192190

193191
#[inline]
194192
fn from_str(s: &str) -> Result<Self, Self::Err> {
195-
Inner::from_str(s).map(Self).map_err(|_| ())
193+
Inner::from_str(s)
194+
.map(Self)
195+
.map_err(|_| crate::Error::BufferOverflow)
196196
}
197197
}
198198

@@ -352,7 +352,7 @@ impl<const N: usize> Ord for String<N> {
352352
macro_rules! impl_try_from_num {
353353
($num:ty, $size:expr) => {
354354
impl<const N: usize> core::convert::TryFrom<$num> for String<N> {
355-
type Error = ();
355+
type Error = crate::Error;
356356
#[inline]
357357
fn try_from(s: $num) -> Result<Self, Self::Error> {
358358
#[cfg(feature = "alloc")]
@@ -361,7 +361,9 @@ macro_rules! impl_try_from_num {
361361
}
362362
#[cfg(not(feature = "alloc"))]
363363
{
364-
Inner::try_from(s).map(Self)
364+
Inner::try_from(s)
365+
.map(Self)
366+
.map_err(|_| crate::Error::BufferOverflow)
365367
}
366368
}
367369
}

src/vec.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<T, const N: usize> Vec<T, N> {
3535

3636
/// Constructs a new vector with a capacity of `N` and fills it with the provided slice.
3737
#[inline]
38-
pub fn from_slice(other: &[T]) -> Result<Self, ()>
38+
pub fn from_slice(other: &[T]) -> crate::Result<Self>
3939
where
4040
T: Clone,
4141
{
@@ -113,7 +113,7 @@ impl<T, const N: usize> Vec<T, N> {
113113
/// Iterates over the slice `other`, clones each element, and then appends
114114
/// it to this `Vec`. The `other` vector is traversed in-order.
115115
#[inline]
116-
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()>
116+
pub fn extend_from_slice(&mut self, other: &[T]) -> crate::Result<()>
117117
where
118118
T: Clone,
119119
{
@@ -125,7 +125,9 @@ impl<T, const N: usize> Vec<T, N> {
125125
}
126126
#[cfg(not(feature = "alloc"))]
127127
{
128-
self.0.extend_from_slice(other)
128+
self.0
129+
.extend_from_slice(other)
130+
.map_err(|_| crate::Error::BufferOverflow)
129131
}
130132
}
131133

@@ -183,7 +185,7 @@ impl<T, const N: usize> Vec<T, N> {
183185
///
184186
/// See also [`resize_default`](Self::resize_default).
185187
#[inline]
186-
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()>
188+
pub fn resize(&mut self, new_len: usize, value: T) -> crate::Result<()>
187189
where
188190
T: Clone,
189191
{
@@ -195,7 +197,9 @@ impl<T, const N: usize> Vec<T, N> {
195197
}
196198
#[cfg(not(feature = "alloc"))]
197199
{
198-
self.0.resize(new_len, value)
200+
self.0
201+
.resize(new_len, value)
202+
.map_err(|_| crate::Error::BufferOverflow)
199203
}
200204
}
201205

@@ -207,7 +211,7 @@ impl<T, const N: usize> Vec<T, N> {
207211
///
208212
/// See also [`resize`](Self::resize).
209213
#[inline]
210-
pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()>
214+
pub fn resize_default(&mut self, new_len: usize) -> crate::Result<()>
211215
where
212216
T: Clone + Default,
213217
{
@@ -342,7 +346,7 @@ impl<const N: usize> fmt::Write for Vec<u8, N> {
342346
}
343347

344348
impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> {
345-
type Error = ();
349+
type Error = crate::Error;
346350

347351
#[inline]
348352
fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)