testdata_rt/
test_input.rs1use std::convert::Infallible;
2use std::error::Error;
3use std::str::{self, Utf8Error};
4
5pub trait TestInput: Sized {
6 type Err: Error;
7 fn try_read_from(data: &[u8]) -> Result<Self, Self::Err>;
8 fn read_from(data: &[u8]) -> Self {
9 Self::try_read_from(data).unwrap()
10 }
11}
12
13impl<T> TestInput for Box<T>
14where
15 T: TestInput,
16{
17 type Err = T::Err;
18 fn try_read_from(data: &[u8]) -> Result<Self, Self::Err> {
19 let value = T::try_read_from(data)?;
20 Ok(Box::new(value))
21 }
22}
23
24impl TestInput for Vec<u8> {
25 type Err = Infallible;
26 fn try_read_from(data: &[u8]) -> Result<Self, Self::Err> {
27 Ok(data.to_owned())
28 }
29}
30
31impl TestInput for String {
32 type Err = Utf8Error;
33 fn try_read_from(data: &[u8]) -> Result<Self, Self::Err> {
34 let s = str::from_utf8(data)?;
35 Ok(s.to_owned())
36 }
37}