|
@@ -114,7 +114,7 @@ fn write_people() -> Result<()> {
|
|
|
name: name.to_owned()
|
|
name: name.to_owned()
|
|
|
})
|
|
})
|
|
|
.collect::<Vec<_>>();
|
|
.collect::<Vec<_>>();
|
|
|
- let bytes = people.to_bytes::<u16>()?;
|
|
|
|
|
|
|
+ let bytes = people.write_to_bytes::<u16>()?;
|
|
|
|
|
|
|
|
assert_eq!(bytes, b"\x03\x00\x04John\x03Bob\x05Harry");
|
|
assert_eq!(bytes, b"\x03\x00\x04John\x03Bob\x05Harry");
|
|
|
|
|
|
|
@@ -124,7 +124,7 @@ fn write_people() -> Result<()> {
|
|
|
#[test]
|
|
#[test]
|
|
|
fn string_vec_transmutation() -> Result<()> {
|
|
fn string_vec_transmutation() -> Result<()> {
|
|
|
let num_vec = vec![b'H', b'e', b'l', b'l', b'o'];
|
|
let num_vec = vec![b'H', b'e', b'l', b'l', b'o'];
|
|
|
- let bytes = num_vec.to_bytes::<u8>()?;
|
|
|
|
|
|
|
+ let bytes = num_vec.write_to_bytes::<u8>()?;
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
|
assert_eq!("Hello".to_owned(), String::read::<u8>(&mut bytes)?);
|
|
assert_eq!("Hello".to_owned(), String::read::<u8>(&mut bytes)?);
|
|
@@ -142,7 +142,7 @@ fn hashmap() -> Result<()> {
|
|
|
(Name("Bread".to_owned()), 12)
|
|
(Name("Bread".to_owned()), 12)
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- let bytes = hashmap.to_bytes::<u16>()?;
|
|
|
|
|
|
|
+ let bytes = hashmap.write_to_bytes::<u16>()?;
|
|
|
assert_eq!(bytes, b"\x02\x00\x05Bread\x0c\x03Egg\x05");
|
|
assert_eq!(bytes, b"\x02\x00\x05Bread\x0c\x03Egg\x05");
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -159,7 +159,7 @@ fn ignored_struct_field() -> Result<()> {
|
|
|
tasks_done: 3
|
|
tasks_done: 3
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- let bytes = crewmate.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = crewmate.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x06\x00Yellow\x03");
|
|
assert_eq!(bytes, b"\x06\x00Yellow\x03");
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -179,7 +179,7 @@ fn time_tests() -> Result<()> {
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
for (duration, expected) in tests {
|
|
for (duration, expected) in tests {
|
|
|
- let bytes = duration.to_bytes::<u32>()?;
|
|
|
|
|
|
|
+ let bytes = duration.write_to_bytes::<u32>()?;
|
|
|
assert_eq!(bytes, expected);
|
|
assert_eq!(bytes, expected);
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -187,7 +187,7 @@ fn time_tests() -> Result<()> {
|
|
|
|
|
|
|
|
// not hardcoding a time since that would be Very Unlikely to pass
|
|
// not hardcoding a time since that would be Very Unlikely to pass
|
|
|
let time = UNIX_EPOCH + duration;
|
|
let time = UNIX_EPOCH + duration;
|
|
|
- let bytes = time.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = time.write_to_bytes()?;
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
|
assert_eq!(time, SystemTime::read(&mut bytes)?);
|
|
assert_eq!(time, SystemTime::read(&mut bytes)?);
|
|
@@ -210,7 +210,7 @@ fn egg_array() -> Result<()> {
|
|
|
}
|
|
}
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- let bytes = eggs.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = eggs.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x01\x00\x01");
|
|
assert_eq!(bytes, b"\x01\x00\x01");
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -224,7 +224,7 @@ fn egg_array() -> Result<()> {
|
|
|
fn usize_is_u64() -> Result<()> {
|
|
fn usize_is_u64() -> Result<()> {
|
|
|
let n: usize = 0;
|
|
let n: usize = 0;
|
|
|
|
|
|
|
|
- let bytes = n.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = n.write_to_bytes()?;
|
|
|
assert_eq!(bytes.len(), 8);
|
|
assert_eq!(bytes.len(), 8);
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -252,7 +252,7 @@ fn giga_enum() -> Result<()> {
|
|
|
GigaEnum::Unit
|
|
GigaEnum::Unit
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- let bytes = enums.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = enums.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x00\x04\x00chad\x01\x0c\x02");
|
|
assert_eq!(bytes, b"\x00\x04\x00chad\x01\x0c\x02");
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -267,7 +267,7 @@ fn ignored_unnamed_fields() -> Result<()> {
|
|
|
struct Real(u8, #[io(ignore)] u16, u32);
|
|
struct Real(u8, #[io(ignore)] u16, u32);
|
|
|
|
|
|
|
|
let mut real = Real(17, 39, 263064);
|
|
let mut real = Real(17, 39, 263064);
|
|
|
- let bytes = real.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = real.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x11\x98\x03\x04\x00");
|
|
assert_eq!(bytes, b"\x11\x98\x03\x04\x00");
|
|
|
|
|
|
|
|
// reading back ignored fields replaces them with default
|
|
// reading back ignored fields replaces them with default
|
|
@@ -285,7 +285,7 @@ fn u64_len() -> Result<()> {
|
|
|
struct Big(#[io(len(u64))] Vec<u8>);
|
|
struct Big(#[io(len(u64))] Vec<u8>);
|
|
|
|
|
|
|
|
let big = Big(vec![]);
|
|
let big = Big(vec![]);
|
|
|
- let bytes = big.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = big.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x00\x00\x00\x00\x00\x00\x00\x00");
|
|
assert_eq!(bytes, b"\x00\x00\x00\x00\x00\x00\x00\x00");
|
|
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
@@ -296,7 +296,7 @@ fn tuples() -> Result<()> {
|
|
|
type Tuple = (u8, u16, u8);
|
|
type Tuple = (u8, u16, u8);
|
|
|
|
|
|
|
|
let tuple = (4, 512, 42);
|
|
let tuple = (4, 512, 42);
|
|
|
- let bytes = tuple.to_bytes()?;
|
|
|
|
|
|
|
+ let bytes = tuple.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x04\x00\x02\x2a");
|
|
assert_eq!(bytes, b"\x04\x00\x02\x2a");
|
|
|
|
|
|
|
|
let mut bytes = &bytes[..];
|
|
let mut bytes = &bytes[..];
|
|
@@ -314,13 +314,76 @@ fn options() -> Result<()> {
|
|
|
|
|
|
|
|
let bytes = Optional {
|
|
let bytes = Optional {
|
|
|
thing: None
|
|
thing: None
|
|
|
- }.to_bytes()?;
|
|
|
|
|
|
|
+ }.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x00");
|
|
assert_eq!(bytes, b"\x00");
|
|
|
|
|
|
|
|
let bytes = Optional {
|
|
let bytes = Optional {
|
|
|
thing: Some(1234)
|
|
thing: Some(1234)
|
|
|
- }.to_bytes()?;
|
|
|
|
|
|
|
+ }.write_to_bytes()?;
|
|
|
assert_eq!(bytes, b"\x01\xd2\x04");
|
|
assert_eq!(bytes, b"\x01\xd2\x04");
|
|
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+#[test]
|
|
|
|
|
+fn results() -> Result<()> {
|
|
|
|
|
+ type R = std::result::Result::<u16, u8>;
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = R::write_to_bytes(&Ok(512))?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x01\x00\x02");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = R::write_to_bytes(&Err(64))?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x00\x40");
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[test]
|
|
|
|
|
+fn str_slice() -> Result<()> {
|
|
|
|
|
+ let bytes = "".write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x00");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = "Hello".write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x05Hello");
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[cfg(feature = "char")]
|
|
|
|
|
+#[test]
|
|
|
|
|
+fn char_array_slice() -> Result<()> {
|
|
|
|
|
+ let arr = ['a', 'b', 'c', 'd', 'e'];
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[0..0]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x00");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[..]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x05abcde");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[1..]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x04bcde");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[1..4]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x03bcd");
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[test]
|
|
|
|
|
+fn array_slice() -> Result<()> {
|
|
|
|
|
+ let arr = [b'a', b'b', b'c', b'd', b'e'];
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[0..0]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x00");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[..]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x05abcde");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[1..]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x04bcde");
|
|
|
|
|
+
|
|
|
|
|
+ let bytes = (&arr[1..4]).write_to_bytes::<u8>()?;
|
|
|
|
|
+ assert_eq!(bytes, b"\x03bcd");
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+}
|