@@ -5,6 +5,11 @@ use crate::io::{ErrorKind, Result, Write};
5
5
pub ( super ) const DEFAULT_SERIALIZER_CAPACITY : usize = 1024 ;
6
6
7
7
/// Serialize an object into a vector of bytes.
8
+ /// # Example
9
+ ///
10
+ /// ```
11
+ /// assert_eq!(vec![12, 0, 0, 0, 0, 0, 0, 0], borsh::to_vec(&12u64).unwrap());
12
+ /// ```
8
13
pub fn to_vec < T > ( value : & T ) -> Result < Vec < u8 > >
9
14
where
10
15
T : BorshSerialize + ?Sized ,
15
20
}
16
21
17
22
/// Serializes an object directly into a `Writer`.
23
+ /// # Example
24
+ ///
25
+ /// ```
26
+ /// # #[cfg(feature = "std")]
27
+ /// let stderr = std::io::stderr();
28
+ /// # #[cfg(feature = "std")]
29
+ /// assert_eq!((), borsh::to_writer(&stderr, "hello_0x0a").unwrap());
30
+ /// ```
18
31
pub fn to_writer < T , W : Write > ( mut writer : W , value : & T ) -> Result < ( ) >
19
32
where
20
33
T : BorshSerialize + ?Sized ,
23
36
}
24
37
25
38
/// Serializes an object without allocation to compute and return its length
39
+ /// # Example
40
+ ///
41
+ /// ```
42
+ /// use borsh::BorshSerialize;
43
+ ///
44
+ /// /// derive is only available if borsh is built with `features = ["derive"]`
45
+ /// # #[cfg(feature = "derive")]
46
+ /// #[derive(BorshSerialize)]
47
+ /// struct A {
48
+ /// tag: String,
49
+ /// value: u64,
50
+ /// };
51
+ ///
52
+ /// # #[cfg(feature = "derive")]
53
+ /// let a = A { tag: "hello".to_owned(), value: 42 };
54
+ ///
55
+ /// assert_eq!(8, borsh::object_length(&12u64).unwrap());
56
+ /// # #[cfg(feature = "derive")]
57
+ /// assert_eq!(17, borsh::object_length(&a).unwrap());
58
+ /// ```
26
59
pub fn object_length < T > ( value : & T ) -> Result < usize >
27
60
where
28
61
T : BorshSerialize + ?Sized ,
0 commit comments