@@ -61,6 +61,7 @@ impl<'d> Data<'d> {
6161}
6262
6363/// The buffer has an invalid size (must be a non null multiple of 2).
64+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
6465pub struct DataFromBufferError ;
6566
6667impl < ' buffer > TryFrom < & ' buffer [ u8 ] > for Data < ' buffer > {
@@ -203,4 +204,59 @@ mod tests {
203204 assert ! ( data_iter. next( ) . is_some( ) ) ;
204205 assert ! ( data_iter. next( ) . is_none( ) ) ;
205206 }
207+
208+ #[ test]
209+ fn data_try_from ( ) {
210+ assert_eq ! (
211+ Data :: try_from( & [ ] as & [ u8 ] ) ,
212+ Err ( DataFromBufferError ) ,
213+ "Data from empty buffer is not allowed"
214+ ) ;
215+ assert_eq ! (
216+ Data :: try_from( & [ 0u8 ] as & [ u8 ] ) ,
217+ Err ( DataFromBufferError ) ,
218+ "Data from buffer with length that is not a multiple of 2 is not allowed"
219+ ) ;
220+ assert_eq ! (
221+ Data :: try_from( & [ 0u8 , 1 , 2 ] as & [ u8 ] ) ,
222+ Err ( DataFromBufferError ) ,
223+ "Data from buffer with length that is not a multiple of 2 is not allowed"
224+ ) ;
225+ assert_eq ! (
226+ Data :: try_from( & [ 0u8 , 1 ] as & [ u8 ] ) ,
227+ Ok ( Data {
228+ data: & [ 0 , 1 ] ,
229+ quantity: 1
230+ } ) ,
231+ "Data from buffer with even length must succeed"
232+ ) ;
233+ assert_eq ! (
234+ Data :: try_from( & 0x1234_5678_u64 . to_be_bytes( ) as & [ u8 ] ) ,
235+ Ok ( Data {
236+ data: & 0x1234_5678_u64 . to_be_bytes( ) ,
237+ quantity: 4
238+ } ) ,
239+ "Data from buffer with even length must succeed"
240+ ) ;
241+ }
242+
243+ #[ test]
244+ fn data_from ( ) {
245+ assert_eq ! (
246+ Data :: from( & [ 0u8 , 1 ] ) ,
247+ Data {
248+ data: & [ 0 , 1 ] ,
249+ quantity: 1
250+ } ,
251+ "Data from buffer with even length must succeed"
252+ ) ;
253+ assert_eq ! (
254+ Data :: from( & 0x1234_5678_u64 . to_be_bytes( ) ) ,
255+ Data {
256+ data: & 0x1234_5678_u64 . to_be_bytes( ) ,
257+ quantity: 4
258+ } ,
259+ "Data from buffer with even length must succeed"
260+ ) ;
261+ }
206262}
0 commit comments