Skip to content

Commit 64fee10

Browse files
authored
Merge pull request #533 from newAM/fix-clippy-result-unit-error
Remove allow(clippy::result_unit_err)
2 parents 4e31d3e + 8566b6c commit 64fee10

File tree

5 files changed

+67
-39
lines changed

5 files changed

+67
-39
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4444

4545
### Changed
4646

47+
- Changed the error type of these methods from `()` to `CapacityError`.
48+
- `String::push_str`
49+
- `String::push`
50+
- `Vec::extend_from_slice`
51+
- `Vec::from_slice`
52+
- `Vec::resize_default`
53+
- `Vec::resize`
54+
- Renamed `FromUtf16Error::DecodeUtf16Error` to `FromUtf16Error::DecodeUtf16`.
4755
- Changed `stable_deref_trait` to a platform-dependent dependency.
4856
- Changed `SortedLinkedList::pop` return type from `Result<T, ()>` to `Option<T>` to match `std::vec::pop`.
4957
- `Vec::capacity` is no longer a `const` function.

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,16 @@ mod ufmt;
233233
pub mod _export {
234234
pub use crate::string::format;
235235
}
236+
237+
/// The error type for fallible [`Vec`] and [`String`] methods.
238+
#[derive(Debug)]
239+
#[non_exhaustive]
240+
pub struct CapacityError;
241+
242+
impl core::fmt::Display for CapacityError {
243+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
244+
f.write_str("insufficient capacity")
245+
}
246+
}
247+
248+
impl core::error::Error for CapacityError {}

src/string/mod.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use core::{
1212
};
1313

1414
use crate::vec::{OwnedVecStorage, Vec, VecInner, VecStorage, ViewVecStorage};
15+
use crate::CapacityError;
1516

1617
mod drain;
1718
pub use drain::Drain;
@@ -24,20 +25,28 @@ pub use drain::Drain;
2425
#[derive(Debug)]
2526
pub enum FromUtf16Error {
2627
/// The capacity of the `String` is too small for the given operation.
27-
Capacity,
28+
Capacity(CapacityError),
2829
/// Error decoding UTF-16.
29-
DecodeUtf16Error(DecodeUtf16Error),
30+
DecodeUtf16(DecodeUtf16Error),
3031
}
3132

3233
impl fmt::Display for FromUtf16Error {
3334
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3435
match self {
35-
Self::Capacity => "insufficient capacity".fmt(f),
36-
Self::DecodeUtf16Error(e) => write!(f, "invalid UTF-16: {}", e),
36+
Self::Capacity(err) => write!(f, "{err}"),
37+
Self::DecodeUtf16(err) => write!(f, "invalid UTF-16: {err}"),
3738
}
3839
}
3940
}
4041

42+
impl core::error::Error for FromUtf16Error {}
43+
44+
impl From<CapacityError> for FromUtf16Error {
45+
fn from(e: CapacityError) -> Self {
46+
Self::Capacity(e)
47+
}
48+
}
49+
4150
/// Base struct for [`String`] and [`StringView`], generic over the [`VecStorage`].
4251
///
4352
/// In most cases you should use [`String`] or [`StringView`] directly. Only use this
@@ -164,10 +173,10 @@ impl<const N: usize> String<N> {
164173
for c in char::decode_utf16(v.iter().cloned()) {
165174
match c {
166175
Ok(c) => {
167-
s.push(c).map_err(|_| FromUtf16Error::Capacity)?;
176+
s.push(c).map_err(|_| CapacityError)?;
168177
}
169178
Err(err) => {
170-
return Err(FromUtf16Error::DecodeUtf16Error(err));
179+
return Err(FromUtf16Error::DecodeUtf16(err));
171180
}
172181
}
173182
}
@@ -253,7 +262,7 @@ impl<const N: usize> String<N> {
253262
/// assert!(b.len() == 2);
254263
///
255264
/// assert_eq!(&[b'a', b'b'], &b[..]);
256-
/// # Ok::<(), ()>(())
265+
/// # Ok::<(), heapless::CapacityError>(())
257266
/// ```
258267
#[inline]
259268
pub fn into_bytes(self) -> Vec<u8, N> {
@@ -360,7 +369,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
360369
///
361370
/// let _s = s.as_str();
362371
/// // s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable
363-
/// # Ok::<(), ()>(())
372+
/// # Ok::<(), heapless::CapacityError>(())
364373
/// ```
365374
#[inline]
366375
pub fn as_str(&self) -> &str {
@@ -379,7 +388,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
379388
/// let mut s: String<4> = String::try_from("ab")?;
380389
/// let s = s.as_mut_str();
381390
/// s.make_ascii_uppercase();
382-
/// # Ok::<(), ()>(())
391+
/// # Ok::<(), heapless::CapacityError>(())
383392
/// ```
384393
#[inline]
385394
pub fn as_mut_str(&mut self) -> &mut str {
@@ -411,7 +420,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
411420
/// vec.reverse();
412421
/// }
413422
/// assert_eq!(s, "olleh");
414-
/// # Ok::<(), ()>(())
423+
/// # Ok::<(), heapless::CapacityError>(())
415424
/// ```
416425
pub unsafe fn as_mut_vec(&mut self) -> &mut VecInner<u8, S> {
417426
&mut self.vec
@@ -433,11 +442,10 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
433442
/// assert_eq!("foobar", s);
434443
///
435444
/// assert!(s.push_str("tender").is_err());
436-
/// # Ok::<(), ()>(())
445+
/// # Ok::<(), heapless::CapacityError>(())
437446
/// ```
438447
#[inline]
439-
#[allow(clippy::result_unit_err)]
440-
pub fn push_str(&mut self, string: &str) -> Result<(), ()> {
448+
pub fn push_str(&mut self, string: &str) -> Result<(), CapacityError> {
441449
self.vec.extend_from_slice(string.as_bytes())
442450
}
443451

@@ -476,13 +484,12 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
476484
/// assert!("abc123" == s.as_str());
477485
///
478486
/// assert_eq!("abc123", s);
479-
/// # Ok::<(), ()>(())
487+
/// # Ok::<(), heapless::CapacityError>(())
480488
/// ```
481489
#[inline]
482-
#[allow(clippy::result_unit_err)]
483-
pub fn push(&mut self, c: char) -> Result<(), ()> {
490+
pub fn push(&mut self, c: char) -> Result<(), CapacityError> {
484491
match c.len_utf8() {
485-
1 => self.vec.push(c as u8).map_err(|_| {}),
492+
1 => self.vec.push(c as u8).map_err(|_| CapacityError),
486493
_ => self
487494
.vec
488495
.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()),
@@ -513,7 +520,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
513520
/// s.truncate(2);
514521
///
515522
/// assert_eq!("he", s);
516-
/// # Ok::<(), ()>(())
523+
/// # Ok::<(), heapless::CapacityError>(())
517524
/// ```
518525
#[inline]
519526
pub fn truncate(&mut self, new_len: usize) {
@@ -541,7 +548,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
541548
/// assert_eq!(s.pop(), Some('f'));
542549
///
543550
/// assert_eq!(s.pop(), None);
544-
/// Ok::<(), ()>(())
551+
/// Ok::<(), heapless::CapacityError>(())
545552
/// ```
546553
pub fn pop(&mut self) -> Option<char> {
547554
let ch = self.chars().next_back()?;
@@ -615,7 +622,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
615622
/// assert!(s.is_empty());
616623
/// assert_eq!(0, s.len());
617624
/// assert_eq!(8, s.capacity());
618-
/// Ok::<(), ()>(())
625+
/// Ok::<(), heapless::CapacityError>(())
619626
/// ```
620627
#[inline]
621628
pub fn clear(&mut self) {
@@ -630,7 +637,8 @@ impl<const N: usize> Default for String<N> {
630637
}
631638

632639
impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
633-
type Error = ();
640+
type Error = CapacityError;
641+
634642
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
635643
let mut new = Self::new();
636644
new.push_str(s)?;
@@ -639,7 +647,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
639647
}
640648

641649
impl<const N: usize> str::FromStr for String<N> {
642-
type Err = ();
650+
type Err = CapacityError;
643651

644652
fn from_str(s: &str) -> Result<Self, Self::Err> {
645653
let mut new = Self::new();
@@ -912,7 +920,7 @@ impl_try_from_num!(u64, 20);
912920

913921
#[cfg(test)]
914922
mod tests {
915-
use crate::{String, Vec};
923+
use crate::{CapacityError, String, Vec};
916924

917925
#[test]
918926
fn static_new() {
@@ -980,7 +988,7 @@ mod tests {
980988
assert!(s.len() == 3);
981989
assert_eq!(s, "123");
982990

983-
let _: () = String::<2>::try_from("123").unwrap_err();
991+
let _: CapacityError = String::<2>::try_from("123").unwrap_err();
984992
}
985993

986994
#[test]
@@ -991,7 +999,7 @@ mod tests {
991999
assert!(s.len() == 3);
9921000
assert_eq!(s, "123");
9931001

994-
let _: () = String::<2>::from_str("123").unwrap_err();
1002+
let _: CapacityError = String::<2>::from_str("123").unwrap_err();
9951003
}
9961004

9971005
#[test]

src/ufmt.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use crate::{
22
string::StringInner,
33
vec::{VecInner, VecStorage},
4+
CapacityError,
45
};
56
use ufmt_write::uWrite;
67

78
impl<S: VecStorage<u8> + ?Sized> uWrite for StringInner<S> {
8-
type Error = ();
9+
type Error = CapacityError;
910
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
1011
self.push_str(s)
1112
}
1213
}
1314

1415
impl<S: VecStorage<u8> + ?Sized> uWrite for VecInner<u8, S> {
15-
type Error = ();
16+
type Error = CapacityError;
1617
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
1718
self.extend_from_slice(s.as_bytes())
1819
}

src/vec/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use core::{
1111
slice,
1212
};
1313

14+
use crate::CapacityError;
15+
1416
mod drain;
1517

1618
mod storage {
@@ -192,8 +194,7 @@ impl<T, const N: usize> Vec<T, N> {
192194
/// let mut v: Vec<u8, 16> = Vec::new();
193195
/// v.extend_from_slice(&[1, 2, 3]).unwrap();
194196
/// ```
195-
#[allow(clippy::result_unit_err)]
196-
pub fn from_slice(other: &[T]) -> Result<Self, ()>
197+
pub fn from_slice(other: &[T]) -> Result<Self, CapacityError>
197198
where
198199
T: Clone,
199200
{
@@ -516,22 +517,21 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
516517
/// vec.extend_from_slice(&[2, 3, 4]).unwrap();
517518
/// assert_eq!(*vec, [1, 2, 3, 4]);
518519
/// ```
519-
#[allow(clippy::result_unit_err)]
520-
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()>
520+
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), CapacityError>
521521
where
522522
T: Clone,
523523
{
524524
pub fn extend_from_slice_inner<T>(
525525
len: &mut usize,
526526
buf: &mut [MaybeUninit<T>],
527527
other: &[T],
528-
) -> Result<(), ()>
528+
) -> Result<(), CapacityError>
529529
where
530530
T: Clone,
531531
{
532532
if *len + other.len() > buf.len() {
533533
// won't fit in the `Vec`; don't modify anything and return an error
534-
Err(())
534+
Err(CapacityError)
535535
} else {
536536
for elem in other {
537537
unsafe { *buf.get_unchecked_mut(*len) = MaybeUninit::new(elem.clone()) }
@@ -626,13 +626,12 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
626626
/// `new_len` is less than len, the Vec is simply truncated.
627627
///
628628
/// See also [`resize_default`](Self::resize_default).
629-
#[allow(clippy::result_unit_err)]
630-
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()>
629+
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), CapacityError>
631630
where
632631
T: Clone,
633632
{
634633
if new_len > self.capacity() {
635-
return Err(());
634+
return Err(CapacityError);
636635
}
637636

638637
if new_len > self.len {
@@ -653,8 +652,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
653652
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
654653
///
655654
/// See also [`resize`](Self::resize).
656-
#[allow(clippy::result_unit_err)]
657-
pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()>
655+
pub fn resize_default(&mut self, new_len: usize) -> Result<(), CapacityError>
658656
where
659657
T: Clone + Default,
660658
{
@@ -1211,7 +1209,7 @@ impl<T, S: VecStorage<T> + ?Sized> Drop for VecInner<T, S> {
12111209
}
12121210

12131211
impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> {
1214-
type Error = ();
1212+
type Error = CapacityError;
12151213

12161214
fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
12171215
Self::from_slice(slice)

0 commit comments

Comments
 (0)