Add more impl
s for Append
and docs for file store magic
This commit is contained in:
parent
2aa08a5898
commit
4963240599
@ -1,6 +1,7 @@
|
|||||||
use crate::collections::BTreeMap;
|
use crate::collections::BTreeMap;
|
||||||
use crate::collections::BTreeSet;
|
use crate::collections::BTreeSet;
|
||||||
use crate::BlockId;
|
use crate::BlockId;
|
||||||
|
use alloc::vec::Vec;
|
||||||
use bitcoin::{Block, OutPoint, Transaction, TxOut};
|
use bitcoin::{Block, OutPoint, Transaction, TxOut};
|
||||||
|
|
||||||
/// Trait to do something with every txout contained in a structure.
|
/// Trait to do something with every txout contained in a structure.
|
||||||
@ -96,3 +97,24 @@ impl<T: Ord> Append for BTreeSet<T> {
|
|||||||
BTreeSet::is_empty(self)
|
BTreeSet::is_empty(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Append for Vec<T> {
|
||||||
|
fn append(&mut self, mut other: Self) {
|
||||||
|
Vec::append(self, &mut other)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
Vec::is_empty(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Append, B: Append> Append for (A, B) {
|
||||||
|
fn append(&mut self, other: Self) {
|
||||||
|
Append::append(&mut self.0, other.0);
|
||||||
|
Append::append(&mut self.1, other.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
Append::is_empty(&self.0) && Append::is_empty(&self.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -47,11 +47,14 @@ where
|
|||||||
///
|
///
|
||||||
/// The file must have been opened with read and write permissions.
|
/// The file must have been opened with read and write permissions.
|
||||||
///
|
///
|
||||||
|
/// `magic` is the expected prefixed bytes of the file. If this does not match, an error will be
|
||||||
|
/// returned.
|
||||||
|
///
|
||||||
/// [`File`]: std::fs::File
|
/// [`File`]: std::fs::File
|
||||||
pub fn new(magic: &'a [u8], mut db_file: File) -> Result<Self, FileError> {
|
pub fn new(magic: &'a [u8], mut db_file: File) -> Result<Self, FileError> {
|
||||||
db_file.rewind()?;
|
db_file.rewind()?;
|
||||||
|
|
||||||
let mut magic_buf = Vec::from_iter((0..).take(magic.len()));
|
let mut magic_buf = vec![0_u8; magic.len()];
|
||||||
db_file.read_exact(magic_buf.as_mut())?;
|
db_file.read_exact(magic_buf.as_mut())?;
|
||||||
|
|
||||||
if magic_buf != magic {
|
if magic_buf != magic {
|
||||||
@ -71,6 +74,10 @@ where
|
|||||||
/// Creates or loads a store from `db_path`.
|
/// Creates or loads a store from `db_path`.
|
||||||
///
|
///
|
||||||
/// If no file exists there, it will be created.
|
/// If no file exists there, it will be created.
|
||||||
|
///
|
||||||
|
/// Refer to [`new`] for documentation on the `magic` input.
|
||||||
|
///
|
||||||
|
/// [`new`]: Self::new
|
||||||
pub fn new_from_path<P>(magic: &'a [u8], db_path: P) -> Result<Self, FileError>
|
pub fn new_from_path<P>(magic: &'a [u8], db_path: P) -> Result<Self, FileError>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
@ -170,46 +177,7 @@ mod test {
|
|||||||
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
|
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
|
||||||
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];
|
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];
|
||||||
|
|
||||||
#[derive(
|
type TestChangeSet = Vec<String>;
|
||||||
Debug,
|
|
||||||
Clone,
|
|
||||||
Copy,
|
|
||||||
PartialOrd,
|
|
||||||
Ord,
|
|
||||||
PartialEq,
|
|
||||||
Eq,
|
|
||||||
Hash,
|
|
||||||
serde::Serialize,
|
|
||||||
serde::Deserialize,
|
|
||||||
)]
|
|
||||||
enum TestKeychain {
|
|
||||||
External,
|
|
||||||
Internal,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl core::fmt::Display for TestKeychain {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::External => write!(f, "external"),
|
|
||||||
Self::Internal => write!(f, "internal"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
|
||||||
struct TestChangeSet {
|
|
||||||
pub changes: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Append for TestChangeSet {
|
|
||||||
fn append(&mut self, mut other: Self) {
|
|
||||||
self.changes.append(&mut other.changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_empty(&self) -> bool {
|
|
||||||
self.changes.is_empty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct TestTracker;
|
struct TestTracker;
|
||||||
@ -248,9 +216,7 @@ mod test {
|
|||||||
let mut data = [255_u8; 2000];
|
let mut data = [255_u8; 2000];
|
||||||
data[..TEST_MAGIC_BYTES_LEN].copy_from_slice(&TEST_MAGIC_BYTES);
|
data[..TEST_MAGIC_BYTES_LEN].copy_from_slice(&TEST_MAGIC_BYTES);
|
||||||
|
|
||||||
let changeset = TestChangeSet {
|
let changeset = vec!["one".into(), "two".into(), "three!".into()];
|
||||||
changes: vec!["one".into(), "two".into(), "three!".into()],
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut file = NamedTempFile::new().unwrap();
|
let mut file = NamedTempFile::new().unwrap();
|
||||||
file.write_all(&data).expect("should write");
|
file.write_all(&data).expect("should write");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user