Explorar el Código

ignore value of Len try_from error, lets u64 be used as Len

deltanedas hace 2 años
padre
commit
3f1a6a5606
Se han modificado 3 ficheros con 16 adiciones y 12 borrados
  1. 1 1
      Cargo.toml
  2. 12 5
      src/impls.rs
  3. 3 6
      src/io.rs

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "orio"
-version = "0.4.0"
+version = "0.4.1"
 authors = ["deltanedas"]
 edition = "2021"
 readme = "README.md"

+ 12 - 5
src/impls.rs

@@ -163,6 +163,7 @@ impl LenIo for String {
 
 	fn write<L: Len>(&self, w: &mut Writer) -> Result<()> {
 		let len = L::try_from(self.len() as u64)
+			.map_err(|_| "conversion to u64 failed")
 			.map_err(other)?;
 		len.write(w)?;
 		w.write_all(self.as_bytes())
@@ -184,7 +185,7 @@ impl<T: Io> LenIo for Vec<T> {
 
 	fn write<L: Len>(&self, w: &mut Writer) -> Result<()> {
 		let len = L::try_from(self.len() as u64)
-			.map_err(other)?;
+			.map_err(conversion)?;
 		len.write(w)?;
 		for item in self {
 			item.write(w)?;
@@ -210,7 +211,7 @@ impl<K: Hash + Io + Ord, V: Io> LenIo for HashMap<K, V> {
 
 	fn write<L: Len>(&self, w: &mut Writer) -> Result<()> {
 		let len = L::try_from(self.len() as u64)
-			.map_err(other)?;
+			.map_err(conversion)?;
 		len.write(w)?;
 		// sorting has some overhead though probably not much overall
 		let mut keys = self.keys().collect::<Vec<_>>();
@@ -252,9 +253,10 @@ impl LenIo for Duration {
 	}
 
 	fn write<L: Len>(&self, w: &mut Writer) -> Result<()> {
-		u64::try_from(self.as_millis())
-			.and_then(L::try_from)
-			.map_err(other)?
+		let ms = u64::try_from(self.as_millis())
+			.map_err(other)?;
+		L::try_from(ms)
+			.map_err(conversion)?
 			.write(w)
 	}
 
@@ -312,3 +314,8 @@ impl<T: Io, const N: usize> Io for [T; N] {
 fn other(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Error {
 	Error::new(ErrorKind::Other, error)
 }
+
+// discard error and use a string
+fn conversion<T>(_: T) -> Error {
+	other("failed to convert length to u64")
+}

+ 3 - 6
src/io.rs

@@ -1,7 +1,4 @@
-use std::{
-	io::Result,
-	num::TryFromIntError
-};
+use std::io::Result;
 
 pub use byteorder::{ReadBytesExt, WriteBytesExt};
 
@@ -61,6 +58,6 @@ pub trait LenIo : Sized {
 
 /// Any primitive integer that can be written.
 /// Used for `LenIo` functions.
-pub trait Len : Io + Into<u64> + TryFrom<u64, Error = TryFromIntError> {}
+pub trait Len : Io + Into<u64> + TryFrom<u64> {}
 
-impl <T: Io + Into<u64> + TryFrom<u64, Error = TryFromIntError>> Len for T {}
+impl <T: Io + Into<u64> + TryFrom<u64>> Len for T {}