Skip to content

Commit a1e2c95

Browse files
committed
Remove unit errors from String and Vec
1 parent 3c97d2f commit a1e2c95

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

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 = String::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 = String::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/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 = Vec::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
@@ -524,28 +523,27 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
524523
/// vec.extend_from_slice(&[2, 3, 4]).unwrap();
525524
/// assert_eq!(*vec, [1, 2, 3, 4]);
526525
/// ```
527-
#[allow(clippy::result_unit_err)]
528-
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()>
526+
pub fn extend_from_slice(&mut self, other: &[T]) -> Option<()>
529527
where
530528
T: Clone,
531529
{
532530
pub fn extend_from_slice_inner<T>(
533531
len: &mut usize,
534532
buf: &mut [MaybeUninit<T>],
535533
other: &[T],
536-
) -> Result<(), ()>
534+
) -> Option<()>
537535
where
538536
T: Clone,
539537
{
540538
if *len + other.len() > buf.len() {
541539
// won't fit in the `Vec`; don't modify anything and return an error
542-
Err(())
540+
None
543541
} else {
544542
for elem in other {
545543
unsafe { *buf.get_unchecked_mut(*len) = MaybeUninit::new(elem.clone()) }
546544
*len += 1;
547545
}
548-
Ok(())
546+
Some(())
549547
}
550548
}
551549

@@ -634,13 +632,12 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
634632
/// new_len is less than len, the Vec is simply truncated.
635633
///
636634
/// See also [`resize_default`](Self::resize_default).
637-
#[allow(clippy::result_unit_err)]
638-
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()>
635+
pub fn resize(&mut self, new_len: usize, value: T) -> Option<()>
639636
where
640637
T: Clone,
641638
{
642639
if new_len > self.storage_capacity() {
643-
return Err(());
640+
return None;
644641
}
645642

646643
if new_len > self.len {
@@ -651,7 +648,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
651648
self.truncate(new_len);
652649
}
653650

654-
Ok(())
651+
Some(())
655652
}
656653

657654
/// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
@@ -661,8 +658,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
661658
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
662659
///
663660
/// See also [`resize`](Self::resize).
664-
#[allow(clippy::result_unit_err)]
665-
pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()>
661+
pub fn resize_default(&mut self, new_len: usize) -> Option<()>
666662
where
667663
T: Clone + Default,
668664
{
@@ -1196,10 +1192,7 @@ where
11961192

11971193
impl<S: VecStorage<u8> + ?Sized> fmt::Write for VecInner<u8, S> {
11981194
fn write_str(&mut self, s: &str) -> fmt::Result {
1199-
match self.extend_from_slice(s.as_bytes()) {
1200-
Ok(()) => Ok(()),
1201-
Err(_) => Err(fmt::Error),
1202-
}
1195+
self.extend_from_slice(s.as_bytes()).ok_or(fmt::Error)
12031196
}
12041197
}
12051198

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

12241217
fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
1225-
Vec::from_slice(slice)
1218+
Vec::from_slice(slice).ok_or(())
12261219
}
12271220
}
12281221

@@ -1329,7 +1322,7 @@ where
13291322
self.vec.len() - self.next,
13301323
)
13311324
};
1332-
vec.extend_from_slice(s).ok();
1325+
vec.extend_from_slice(s);
13331326
}
13341327

13351328
Self { vec, next: 0 }
@@ -1843,7 +1836,7 @@ mod tests {
18431836

18441837
v.resize(0, 0).unwrap();
18451838
v.resize(4, 0).unwrap();
1846-
v.resize(5, 0).expect_err("full");
1839+
assert!(v.resize(5, 0).is_none(), "full");
18471840
}
18481841

18491842
#[test]
@@ -1923,7 +1916,7 @@ mod tests {
19231916
v.extend_from_slice(&[3]).unwrap();
19241917
assert_eq!(v.len(), 3);
19251918
assert_eq!(v.as_slice(), &[1, 2, 3]);
1926-
assert!(v.extend_from_slice(&[4, 5]).is_err());
1919+
assert!(v.extend_from_slice(&[4, 5]).is_none());
19271920
assert_eq!(v.len(), 3);
19281921
assert_eq!(v.as_slice(), &[1, 2, 3]);
19291922
}
@@ -1936,7 +1929,7 @@ mod tests {
19361929
assert_eq!(v.as_slice(), &[1, 2, 3]);
19371930

19381931
// Slice too large
1939-
assert!(Vec::<u8, 2>::from_slice(&[1, 2, 3]).is_err());
1932+
assert!(Vec::<u8, 2>::from_slice(&[1, 2, 3]).is_none());
19401933
}
19411934

19421935
#[test]

0 commit comments

Comments
 (0)