فهرست منبع

fix option wrong size and systemtime wrong serialization

deltanedas 2 سال پیش
والد
کامیت
a74802c546
3فایلهای تغییر یافته به همراه26 افزوده شده و 5 حذف شده
  1. 1 1
      Cargo.toml
  2. 5 4
      src/impls.rs
  3. 20 0
      src/tests.rs

+ 1 - 1
Cargo.toml

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

+ 5 - 4
src/impls.rs

@@ -144,8 +144,8 @@ impl<T: Io> Io for Option<T> {
 	}
 
 	fn size(&self) -> usize {
-		match self {
-			Some(inner) => 1 + inner.size(),
+		1 + match self {
+			Some(inner) => inner.size(),
 			None => 0
 		}
 	}
@@ -275,9 +275,10 @@ impl Io for SystemTime {
 	}
 
 	fn write(&self, w: &mut Writer) -> Result<()> {
-		self.duration_since(UNIX_EPOCH)
+		u64::try_from(self.duration_since(UNIX_EPOCH)
 			.map_err(other)
-			.map(|offset| offset.as_millis())?
+			.map(|offset| offset.as_millis())?)
+			.map_err(other)?
 			.write(w)
 	}
 

+ 20 - 0
src/tests.rs

@@ -304,3 +304,23 @@ fn tuples() -> Result<()> {
 
 	Ok(())
 }
+
+#[test]
+fn options() -> Result<()> {
+	#[derive(Io)]
+	struct Optional {
+		thing: Option<u16>
+	}
+
+	let bytes = Optional {
+		thing: None
+	}.to_bytes()?;
+	assert_eq!(bytes, b"\x00");
+
+	let bytes = Optional {
+		thing: Some(1234)
+	}.to_bytes()?;
+	assert_eq!(bytes, b"\x01\xd2\x04");
+
+	Ok(())
+}