Skip to content

Commit e8169ef

Browse files
committed
Move around write for BorshSerialize, test
1 parent a8d7b66 commit e8169ef

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ version = "1.0"
3636
[dev-dependencies]
3737
matches = { version = "0.1" }
3838
bencher = "0.1.4"
39+
borsh = { version = "1.5.5", default-features = false, features = ["derive"] }
3940

4041
[[bench]]
4142
name = "extend"

src/arrayvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ where
14281428
}
14291429
}
14301430

1431-
#[cfg(all(feature = "borsh", not(feature = "std")))]
1431+
#[cfg(all(feature = "borsh"))]
14321432
/// Requires crate feature `"borsh"`
14331433
impl<const CAP: usize> borsh::io::Write for ArrayVec<u8, CAP> {
14341434
fn write(&mut self, data: &[u8]) -> borsh::io::Result<usize> {

tests/borsh.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ mod array_vec {
4646
assert_ser(&vec, b"\x01\0\0\0\xef\xbe\xad\xde");
4747
assert_roundtrip(&vec);
4848
}
49+
50+
#[test]
51+
fn test_borsh_one_to_one() {
52+
use borsh::{BorshDeserialize, BorshSerialize};
53+
#[derive(BorshSerialize, BorshDeserialize)]
54+
struct Something {
55+
a: String,
56+
}
57+
let a = Something {
58+
a: "hello".to_owned(),
59+
};
60+
let mut vec = ArrayVec::<u8, 9>::new();
61+
a.serialize(&mut vec).unwrap();
62+
let mut test = Vec::new();
63+
a.serialize(&mut test).unwrap();
64+
assert_eq!(vec.as_slice(), test.as_slice());
65+
}
66+
67+
#[test]
68+
fn test_borsh_more_than() {
69+
use borsh::{io::ErrorKind, BorshDeserialize, BorshSerialize};
70+
#[derive(BorshSerialize, BorshDeserialize)]
71+
struct Something {
72+
a: String,
73+
}
74+
let mut b = ArrayVec::<u8, 5>::new();
75+
let err = Something {
76+
a: "Hello, world!".to_owned(),
77+
}
78+
.serialize(&mut b)
79+
.unwrap_err();
80+
assert_eq!(ErrorKind::WriteZero, err.kind());
81+
}
4982
}
5083

5184
mod array_string {

0 commit comments

Comments
 (0)