Skip to content

Commit 935dc16

Browse files
committed
Add more impls for Append and docs for file store magic
1 parent 2aa08a5 commit 935dc16

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

crates/chain/src/tx_data_traits.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::collections::BTreeMap;
22
use crate::collections::BTreeSet;
33
use crate::BlockId;
4+
use alloc::vec::Vec;
45
use bitcoin::{Block, OutPoint, Transaction, TxOut};
56

67
/// Trait to do something with every txout contained in a structure.
@@ -96,3 +97,24 @@ impl<T: Ord> Append for BTreeSet<T> {
9697
BTreeSet::is_empty(self)
9798
}
9899
}
100+
101+
impl<T> Append for Vec<T> {
102+
fn append(&mut self, mut other: Self) {
103+
Vec::append(self, &mut other)
104+
}
105+
106+
fn is_empty(&self) -> bool {
107+
Vec::is_empty(self)
108+
}
109+
}
110+
111+
impl<A: Append, B: Append> Append for (A, B) {
112+
fn append(&mut self, other: Self) {
113+
Append::append(&mut self.0, other.0);
114+
Append::append(&mut self.1, other.1);
115+
}
116+
117+
fn is_empty(&self) -> bool {
118+
Append::is_empty(&self.0) && Append::is_empty(&self.1)
119+
}
120+
}

crates/file_store/src/store.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ where
4747
///
4848
/// The file must have been opened with read and write permissions.
4949
///
50+
/// `magic` is the expected prefixed bytes of the file. If this does not match, an error will be
51+
/// returned.
52+
///
5053
/// [`File`]: std::fs::File
5154
pub fn new(magic: &'a [u8], mut db_file: File) -> Result<Self, FileError> {
5255
db_file.rewind()?;
5356

54-
let mut magic_buf = Vec::from_iter((0..).take(magic.len()));
57+
let mut magic_buf = vec![0_u8; magic.len()];
5558
db_file.read_exact(magic_buf.as_mut())?;
5659

5760
if magic_buf != magic {
@@ -71,6 +74,10 @@ where
7174
/// Creates or loads a store from `db_path`.
7275
///
7376
/// If no file exists there, it will be created.
77+
///
78+
/// Refer to [`new`] for documentation on the `magic` input.
79+
///
80+
/// [`new`]: Self::new
7481
pub fn new_from_path<P>(magic: &'a [u8], db_path: P) -> Result<Self, FileError>
7582
where
7683
P: AsRef<Path>,
@@ -170,46 +177,22 @@ mod test {
170177
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
171178
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];
172179

173-
#[derive(
174-
Debug,
175-
Clone,
176-
Copy,
177-
PartialOrd,
178-
Ord,
179-
PartialEq,
180-
Eq,
181-
Hash,
182-
serde::Serialize,
183-
serde::Deserialize,
184-
)]
185-
enum TestKeychain {
186-
External,
187-
Internal,
188-
}
180+
// #[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
181+
// struct TestChangeSet {
182+
// pub changes: Vec<String>,
183+
// }
189184

190-
impl core::fmt::Display for TestKeychain {
191-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
192-
match self {
193-
Self::External => write!(f, "external"),
194-
Self::Internal => write!(f, "internal"),
195-
}
196-
}
197-
}
185+
type TestChangeSet = Vec<String>;
198186

199-
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
200-
struct TestChangeSet {
201-
pub changes: Vec<String>,
202-
}
203-
204-
impl Append for TestChangeSet {
205-
fn append(&mut self, mut other: Self) {
206-
self.changes.append(&mut other.changes)
207-
}
187+
// impl Append for TestChangeSet {
188+
// fn append(&mut self, mut other: Self) {
189+
// self.changes.append(&mut other.changes)
190+
// }
208191

209-
fn is_empty(&self) -> bool {
210-
self.changes.is_empty()
211-
}
212-
}
192+
// fn is_empty(&self) -> bool {
193+
// self.changes.is_empty()
194+
// }
195+
// }
213196

214197
#[derive(Debug)]
215198
struct TestTracker;
@@ -248,9 +231,7 @@ mod test {
248231
let mut data = [255_u8; 2000];
249232
data[..TEST_MAGIC_BYTES_LEN].copy_from_slice(&TEST_MAGIC_BYTES);
250233

251-
let changeset = TestChangeSet {
252-
changes: vec!["one".into(), "two".into(), "three!".into()],
253-
};
234+
let changeset = vec!["one".into(), "two".into(), "three!".into()];
254235

255236
let mut file = NamedTempFile::new().unwrap();
256237
file.write_all(&data).expect("should write");

0 commit comments

Comments
 (0)