Skip to content

Commit 91d19c4

Browse files
committed
Remove unit errors from String and Vec
1 parent bfc9598 commit 91d19c4

File tree

4 files changed

+36
-45
lines changed

4 files changed

+36
-45
lines changed

src/de.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'de, const N: usize> Deserialize<'de> for String<N> {
318318
{
319319
let mut s = String::new();
320320
s.push_str(v)
321-
.map_err(|_| E::invalid_length(v.len(), &self))?;
321+
.ok_or_else(|| E::invalid_length(v.len(), &self))?;
322322
Ok(s)
323323
}
324324

@@ -332,7 +332,7 @@ impl<'de, const N: usize> Deserialize<'de> for String<N> {
332332
core::str::from_utf8(v)
333333
.map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))?,
334334
)
335-
.map_err(|_| E::invalid_length(v.len(), &self))?;
335+
.ok_or_else(|| E::invalid_length(v.len(), &self))?;
336336

337337
Ok(s)
338338
}

src/string/mod.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<const N: usize> String<N> {
164164
for c in char::decode_utf16(v.iter().cloned()) {
165165
match c {
166166
Ok(c) => {
167-
s.push(c).map_err(|_| FromUtf16Error::Capacity)?;
167+
s.push(c).ok_or(FromUtf16Error::Capacity)?;
168168
}
169169
Err(err) => {
170170
return Err(FromUtf16Error::DecodeUtf16Error(err));
@@ -428,16 +428,15 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
428428
///
429429
/// let mut s: String<8> = String::try_from("foo")?;
430430
///
431-
/// assert!(s.push_str("bar").is_ok());
431+
/// assert!(s.push_str("bar").is_some());
432432
///
433433
/// assert_eq!("foobar", s);
434434
///
435-
/// assert!(s.push_str("tender").is_err());
435+
/// assert!(s.push_str("tender").is_none());
436436
/// # Ok::<(), ()>(())
437437
/// ```
438438
#[inline]
439-
#[allow(clippy::result_unit_err)]
440-
pub fn push_str(&mut self, string: &str) -> Result<(), ()> {
439+
pub fn push_str(&mut self, string: &str) -> Option<()> {
441440
self.vec.extend_from_slice(string.as_bytes())
442441
}
443442

@@ -479,10 +478,9 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
479478
/// # Ok::<(), ()>(())
480479
/// ```
481480
#[inline]
482-
#[allow(clippy::result_unit_err)]
483-
pub fn push(&mut self, c: char) -> Result<(), ()> {
481+
pub fn push(&mut self, c: char) -> Option<()> {
484482
match c.len_utf8() {
485-
1 => self.vec.push(c as u8).map_err(|_| {}),
483+
1 => self.vec.push(c as u8).ok(),
486484
_ => self
487485
.vec
488486
.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()),
@@ -633,7 +631,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
633631
type Error = ();
634632
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
635633
let mut new = Self::new();
636-
new.push_str(s)?;
634+
new.push_str(s).ok_or(())?;
637635
Ok(new)
638636
}
639637
}
@@ -643,7 +641,7 @@ impl<const N: usize> str::FromStr for String<N> {
643641

644642
fn from_str(s: &str) -> Result<Self, Self::Err> {
645643
let mut new = Self::new();
646-
new.push_str(s)?;
644+
new.push_str(s).ok_or(())?;
647645
Ok(new)
648646
}
649647
}
@@ -707,11 +705,11 @@ impl<S: VecStorage<u8> + ?Sized> hash::Hash for StringInner<S> {
707705

708706
impl<S: VecStorage<u8> + ?Sized> fmt::Write for StringInner<S> {
709707
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
710-
self.push_str(s).map_err(|_| fmt::Error)
708+
self.push_str(s).ok_or(fmt::Error)
711709
}
712710

713711
fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
714-
self.push(c).map_err(|_| fmt::Error)
712+
self.push(c).ok_or(fmt::Error)
715713
}
716714
}
717715

@@ -1051,21 +1049,21 @@ mod tests {
10511049
#[test]
10521050
fn push_str() {
10531051
let mut s: String<8> = String::try_from("foo").unwrap();
1054-
assert!(s.push_str("bar").is_ok());
1052+
assert!(s.push_str("bar").is_some());
10551053
assert_eq!("foobar", s);
10561054
assert_eq!(s, "foobar");
1057-
assert!(s.push_str("tender").is_err());
1055+
assert!(s.push_str("tender").is_none());
10581056
assert_eq!("foobar", s);
10591057
assert_eq!(s, "foobar");
10601058
}
10611059

10621060
#[test]
10631061
fn push() {
10641062
let mut s: String<6> = String::try_from("abc").unwrap();
1065-
assert!(s.push('1').is_ok());
1066-
assert!(s.push('2').is_ok());
1067-
assert!(s.push('3').is_ok());
1068-
assert!(s.push('4').is_err());
1063+
assert!(s.push('1').is_some());
1064+
assert!(s.push('2').is_some());
1065+
assert!(s.push('3').is_some());
1066+
assert!(s.push('4').is_none());
10691067
assert!("abc123" == s.as_str());
10701068
}
10711069

src/ufmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use ufmt_write::uWrite;
77
impl<S: VecStorage<u8> + ?Sized> uWrite for StringInner<S> {
88
type Error = ();
99
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
10-
self.push_str(s)
10+
self.push_str(s).ok_or_else(|| ())
1111
}
1212
}
1313

1414
impl<S: VecStorage<u8> + ?Sized> uWrite for VecInner<u8, S> {
1515
type Error = ();
1616
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
17-
self.extend_from_slice(s.as_bytes())
17+
self.extend_from_slice(s.as_bytes()).ok_or_else(|| ())
1818
}
1919
}
2020

src/vec/mod.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,13 @@ impl<T, const N: usize> Vec<T, N> {
192192
/// let mut v: Vec<u8, 16> = Vec::new();
193193
/// v.extend_from_slice(&[1, 2, 3]).unwrap();
194194
/// ```
195-
#[allow(clippy::result_unit_err)]
196-
pub fn from_slice(other: &[T]) -> Result<Self, ()>
195+
pub fn from_slice(other: &[T]) -> Option<Self>
197196
where
198197
T: Clone,
199198
{
200199
let mut v = Self::new();
201200
v.extend_from_slice(other)?;
202-
Ok(v)
201+
Some(v)
203202
}
204203

205204
/// Constructs a new vector with a fixed capacity of `N`, initializing
@@ -517,28 +516,27 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
517516
/// vec.extend_from_slice(&[2, 3, 4]).unwrap();
518517
/// assert_eq!(*vec, [1, 2, 3, 4]);
519518
/// ```
520-
#[allow(clippy::result_unit_err)]
521-
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()>
519+
pub fn extend_from_slice(&mut self, other: &[T]) -> Option<()>
522520
where
523521
T: Clone,
524522
{
525523
pub fn extend_from_slice_inner<T>(
526524
len: &mut usize,
527525
buf: &mut [MaybeUninit<T>],
528526
other: &[T],
529-
) -> Result<(), ()>
527+
) -> Option<()>
530528
where
531529
T: Clone,
532530
{
533531
if *len + other.len() > buf.len() {
534532
// won't fit in the `Vec`; don't modify anything and return an error
535-
Err(())
533+
None
536534
} else {
537535
for elem in other {
538536
unsafe { *buf.get_unchecked_mut(*len) = MaybeUninit::new(elem.clone()) }
539537
*len += 1;
540538
}
541-
Ok(())
539+
Some(())
542540
}
543541
}
544542

@@ -627,13 +625,12 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
627625
/// new_len is less than len, the Vec is simply truncated.
628626
///
629627
/// See also [`resize_default`](Self::resize_default).
630-
#[allow(clippy::result_unit_err)]
631-
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()>
628+
pub fn resize(&mut self, new_len: usize, value: T) -> Option<()>
632629
where
633630
T: Clone,
634631
{
635632
if new_len > self.capacity() {
636-
return Err(());
633+
return None;
637634
}
638635

639636
if new_len > self.len {
@@ -644,7 +641,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
644641
self.truncate(new_len);
645642
}
646643

647-
Ok(())
644+
Some(())
648645
}
649646

650647
/// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
@@ -654,8 +651,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
654651
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
655652
///
656653
/// See also [`resize`](Self::resize).
657-
#[allow(clippy::result_unit_err)]
658-
pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()>
654+
pub fn resize_default(&mut self, new_len: usize) -> Option<()>
659655
where
660656
T: Clone + Default,
661657
{
@@ -1189,10 +1185,7 @@ where
11891185

11901186
impl<S: VecStorage<u8> + ?Sized> fmt::Write for VecInner<u8, S> {
11911187
fn write_str(&mut self, s: &str) -> fmt::Result {
1192-
match self.extend_from_slice(s.as_bytes()) {
1193-
Ok(()) => Ok(()),
1194-
Err(_) => Err(fmt::Error),
1195-
}
1188+
self.extend_from_slice(s.as_bytes()).ok_or(fmt::Error)
11961189
}
11971190
}
11981191

@@ -1215,7 +1208,7 @@ impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> {
12151208
type Error = ();
12161209

12171210
fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
1218-
Self::from_slice(slice)
1211+
Self::from_slice(slice).ok_or(())
12191212
}
12201213
}
12211214

@@ -1322,7 +1315,7 @@ where
13221315
self.vec.len() - self.next,
13231316
)
13241317
};
1325-
vec.extend_from_slice(s).ok();
1318+
vec.extend_from_slice(s);
13261319
}
13271320

13281321
Self { vec, next: 0 }
@@ -1836,7 +1829,7 @@ mod tests {
18361829

18371830
v.resize(0, 0).unwrap();
18381831
v.resize(4, 0).unwrap();
1839-
v.resize(5, 0).expect_err("full");
1832+
assert!(v.resize(5, 0).is_none(), "full");
18401833
}
18411834

18421835
#[test]
@@ -1916,7 +1909,7 @@ mod tests {
19161909
v.extend_from_slice(&[3]).unwrap();
19171910
assert_eq!(v.len(), 3);
19181911
assert_eq!(v.as_slice(), &[1, 2, 3]);
1919-
assert!(v.extend_from_slice(&[4, 5]).is_err());
1912+
assert!(v.extend_from_slice(&[4, 5]).is_none());
19201913
assert_eq!(v.len(), 3);
19211914
assert_eq!(v.as_slice(), &[1, 2, 3]);
19221915
}
@@ -1929,7 +1922,7 @@ mod tests {
19291922
assert_eq!(v.as_slice(), &[1, 2, 3]);
19301923

19311924
// Slice too large
1932-
assert!(Vec::<u8, 2>::from_slice(&[1, 2, 3]).is_err());
1925+
assert!(Vec::<u8, 2>::from_slice(&[1, 2, 3]).is_none());
19331926
}
19341927

19351928
#[test]

0 commit comments

Comments
 (0)