1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
pub mod bytes;
pub use bytes::BytesInput;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::{clone::Clone, fmt::Debug};
#[cfg(feature = "std")]
use std::{
fs::File,
io::{Read, Write},
path::Path,
};
use serde::{Deserialize, Serialize};
use crate::{bolts::ownedref::OwnedSlice, Error};
pub trait Input: Clone + serde::Serialize + serde::de::DeserializeOwned + Debug {
#[cfg(feature = "std")]
fn to_file<P>(&self, path: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
let mut file = File::create(path)?;
let serialized = postcard::to_allocvec(self)?;
file.write_all(&serialized)?;
Ok(())
}
#[cfg(not(feature = "std"))]
fn to_file<P>(&self, _path: P) -> Result<(), Error> {
Err(Error::NotImplemented("Not supported in no_std".into()))
}
#[cfg(feature = "std")]
fn from_file<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let mut file = File::open(path)?;
let mut bytes: Vec<u8> = vec![];
file.read_to_end(&mut bytes)?;
Ok(postcard::from_bytes(&bytes)?)
}
#[cfg(not(feature = "std"))]
fn from_file<P>(_path: P) -> Result<Self, Error> {
Err(Error::NotImplemented("Not supprted in no_std".into()))
}
fn generate_name(&self, idx: usize) -> String;
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
pub struct NopInput {}
impl Input for NopInput {
fn generate_name(&self, _idx: usize) -> String {
"nop-input".to_string()
}
}
impl HasTargetBytes for NopInput {
fn target_bytes(&self) -> OwnedSlice<u8> {
OwnedSlice::Owned(vec![0])
}
}
pub trait HasTargetBytes {
fn target_bytes(&self) -> OwnedSlice<u8>;
}
pub trait HasBytesVec {
fn bytes(&self) -> &[u8];
fn bytes_mut(&mut self) -> &mut Vec<u8>;
}
pub trait HasLen {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}