diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c4e1c7bc..23e6eafae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Changed `stable_deref_trait` to a platform-dependent dependency. - Changed `SortedLinkedList::pop` return type from `Result` to `Option` to match `std::vec::pop`. - `Vec::capacity` is no longer a `const` function. +- Changed `String::push_str` return type to `Option<()>`. +- Changed `String::push` return type to `Option<()>`. +- Changed `Vec::from_slice` return type to `Option`. +- Changed `Vec::extend_from_slice` return type to `Option<()>`. +- Changed `Vec::resize` return type to `Option<()>`. +- Changed `Vec::resize_defualt` return type to `Option<()>`. +- Changed `SortedLinkedList::rpop` return type to `Option`. ### Fixed diff --git a/src/de.rs b/src/de.rs index 455295b717..72b1e57c47 100644 --- a/src/de.rs +++ b/src/de.rs @@ -318,7 +318,7 @@ impl<'de, const N: usize> Deserialize<'de> for String { { let mut s = String::new(); s.push_str(v) - .map_err(|_| E::invalid_length(v.len(), &self))?; + .ok_or_else(|| E::invalid_length(v.len(), &self))?; Ok(s) } @@ -332,7 +332,7 @@ impl<'de, const N: usize> Deserialize<'de> for String { core::str::from_utf8(v) .map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))?, ) - .map_err(|_| E::invalid_length(v.len(), &self))?; + .ok_or_else(|| E::invalid_length(v.len(), &self))?; Ok(s) } diff --git a/src/string/mod.rs b/src/string/mod.rs index 6aee8f270f..7cef31a78a 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -164,7 +164,7 @@ impl String { for c in char::decode_utf16(v.iter().cloned()) { match c { Ok(c) => { - s.push(c).map_err(|_| FromUtf16Error::Capacity)?; + s.push(c).ok_or(FromUtf16Error::Capacity)?; } Err(err) => { return Err(FromUtf16Error::DecodeUtf16Error(err)); @@ -428,16 +428,15 @@ impl + ?Sized> StringInner { /// /// let mut s: String<8> = String::try_from("foo")?; /// - /// assert!(s.push_str("bar").is_ok()); + /// assert!(s.push_str("bar").is_some()); /// /// assert_eq!("foobar", s); /// - /// assert!(s.push_str("tender").is_err()); + /// assert!(s.push_str("tender").is_none()); /// # Ok::<(), ()>(()) /// ``` #[inline] - #[allow(clippy::result_unit_err)] - pub fn push_str(&mut self, string: &str) -> Result<(), ()> { + pub fn push_str(&mut self, string: &str) -> Option<()> { self.vec.extend_from_slice(string.as_bytes()) } @@ -479,10 +478,9 @@ impl + ?Sized> StringInner { /// # Ok::<(), ()>(()) /// ``` #[inline] - #[allow(clippy::result_unit_err)] - pub fn push(&mut self, c: char) -> Result<(), ()> { + pub fn push(&mut self, c: char) -> Option<()> { match c.len_utf8() { - 1 => self.vec.push(c as u8).map_err(|_| {}), + 1 => self.vec.push(c as u8).ok(), _ => self .vec .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 { type Error = (); fn try_from(s: &'a str) -> Result { let mut new = Self::new(); - new.push_str(s)?; + new.push_str(s).ok_or(())?; Ok(new) } } @@ -643,7 +641,7 @@ impl str::FromStr for String { fn from_str(s: &str) -> Result { let mut new = Self::new(); - new.push_str(s)?; + new.push_str(s).ok_or(())?; Ok(new) } } @@ -707,11 +705,11 @@ impl + ?Sized> hash::Hash for StringInner { impl + ?Sized> fmt::Write for StringInner { fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { - self.push_str(s).map_err(|_| fmt::Error) + self.push_str(s).ok_or(fmt::Error) } fn write_char(&mut self, c: char) -> Result<(), fmt::Error> { - self.push(c).map_err(|_| fmt::Error) + self.push(c).ok_or(fmt::Error) } } @@ -1051,10 +1049,10 @@ mod tests { #[test] fn push_str() { let mut s: String<8> = String::try_from("foo").unwrap(); - assert!(s.push_str("bar").is_ok()); + assert!(s.push_str("bar").is_some()); assert_eq!("foobar", s); assert_eq!(s, "foobar"); - assert!(s.push_str("tender").is_err()); + assert!(s.push_str("tender").is_none()); assert_eq!("foobar", s); assert_eq!(s, "foobar"); } @@ -1062,10 +1060,10 @@ mod tests { #[test] fn push() { let mut s: String<6> = String::try_from("abc").unwrap(); - assert!(s.push('1').is_ok()); - assert!(s.push('2').is_ok()); - assert!(s.push('3').is_ok()); - assert!(s.push('4').is_err()); + assert!(s.push('1').is_some()); + assert!(s.push('2').is_some()); + assert!(s.push('3').is_some()); + assert!(s.push('4').is_none()); assert!("abc123" == s.as_str()); } diff --git a/src/ufmt.rs b/src/ufmt.rs index e8995b5ffa..e2163e5356 100644 --- a/src/ufmt.rs +++ b/src/ufmt.rs @@ -7,14 +7,14 @@ use ufmt_write::uWrite; impl + ?Sized> uWrite for StringInner { type Error = (); fn write_str(&mut self, s: &str) -> Result<(), Self::Error> { - self.push_str(s) + self.push_str(s).ok_or_else(|| ()) } } impl + ?Sized> uWrite for VecInner { type Error = (); fn write_str(&mut self, s: &str) -> Result<(), Self::Error> { - self.extend_from_slice(s.as_bytes()) + self.extend_from_slice(s.as_bytes()).ok_or_else(|| ()) } } diff --git a/src/vec/mod.rs b/src/vec/mod.rs index 89fae0acf5..809999462d 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -192,14 +192,13 @@ impl Vec { /// let mut v: Vec = Vec::new(); /// v.extend_from_slice(&[1, 2, 3]).unwrap(); /// ``` - #[allow(clippy::result_unit_err)] - pub fn from_slice(other: &[T]) -> Result + pub fn from_slice(other: &[T]) -> Option where T: Clone, { let mut v = Self::new(); v.extend_from_slice(other)?; - Ok(v) + Some(v) } /// Constructs a new vector with a fixed capacity of `N`, initializing @@ -517,8 +516,7 @@ impl + ?Sized> VecInner { /// vec.extend_from_slice(&[2, 3, 4]).unwrap(); /// assert_eq!(*vec, [1, 2, 3, 4]); /// ``` - #[allow(clippy::result_unit_err)] - pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()> + pub fn extend_from_slice(&mut self, other: &[T]) -> Option<()> where T: Clone, { @@ -526,19 +524,19 @@ impl + ?Sized> VecInner { len: &mut usize, buf: &mut [MaybeUninit], other: &[T], - ) -> Result<(), ()> + ) -> Option<()> where T: Clone, { if *len + other.len() > buf.len() { // won't fit in the `Vec`; don't modify anything and return an error - Err(()) + None } else { for elem in other { unsafe { *buf.get_unchecked_mut(*len) = MaybeUninit::new(elem.clone()) } *len += 1; } - Ok(()) + Some(()) } } @@ -627,13 +625,12 @@ impl + ?Sized> VecInner { /// new_len is less than len, the Vec is simply truncated. /// /// See also [`resize_default`](Self::resize_default). - #[allow(clippy::result_unit_err)] - pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()> + pub fn resize(&mut self, new_len: usize, value: T) -> Option<()> where T: Clone, { if new_len > self.capacity() { - return Err(()); + return None; } if new_len > self.len { @@ -644,7 +641,7 @@ impl + ?Sized> VecInner { self.truncate(new_len); } - Ok(()) + Some(()) } /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. @@ -654,8 +651,7 @@ impl + ?Sized> VecInner { /// If `new_len` is less than `len`, the `Vec` is simply truncated. /// /// See also [`resize`](Self::resize). - #[allow(clippy::result_unit_err)] - pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()> + pub fn resize_default(&mut self, new_len: usize) -> Option<()> where T: Clone + Default, { @@ -1189,10 +1185,7 @@ where impl + ?Sized> fmt::Write for VecInner { fn write_str(&mut self, s: &str) -> fmt::Result { - match self.extend_from_slice(s.as_bytes()) { - Ok(()) => Ok(()), - Err(_) => Err(fmt::Error), - } + self.extend_from_slice(s.as_bytes()).ok_or(fmt::Error) } } @@ -1215,7 +1208,7 @@ impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec { type Error = (); fn try_from(slice: &'a [T]) -> Result { - Self::from_slice(slice) + Self::from_slice(slice).ok_or(()) } } @@ -1322,7 +1315,7 @@ where self.vec.len() - self.next, ) }; - vec.extend_from_slice(s).ok(); + vec.extend_from_slice(s); } Self { vec, next: 0 } @@ -1836,7 +1829,7 @@ mod tests { v.resize(0, 0).unwrap(); v.resize(4, 0).unwrap(); - v.resize(5, 0).expect_err("full"); + assert!(v.resize(5, 0).is_none(), "full"); } #[test] @@ -1916,7 +1909,7 @@ mod tests { v.extend_from_slice(&[3]).unwrap(); assert_eq!(v.len(), 3); assert_eq!(v.as_slice(), &[1, 2, 3]); - assert!(v.extend_from_slice(&[4, 5]).is_err()); + assert!(v.extend_from_slice(&[4, 5]).is_none()); assert_eq!(v.len(), 3); assert_eq!(v.as_slice(), &[1, 2, 3]); } @@ -1929,7 +1922,7 @@ mod tests { assert_eq!(v.as_slice(), &[1, 2, 3]); // Slice too large - assert!(Vec::::from_slice(&[1, 2, 3]).is_err()); + assert!(Vec::::from_slice(&[1, 2, 3]).is_none()); } #[test]