Cleanup redundant AsFixedBytes and FixedLength traits (#3131)

* no need for AsFixedBytes we can just use AsRef<[u8]>

* cleanup FixedLength trait

* revert this change for now

* fix store tests

* cleanup and fix tests after rebase

* fix tests

* update based on PR review
less hard-coded values now

* cleanup
This commit is contained in:
Antioch Peverell
2020-01-29 13:41:50 +00:00
committed by GitHub
parent 83a2649946
commit 616dad43fd
15 changed files with 112 additions and 265 deletions
+6 -5
View File
@@ -19,11 +19,12 @@ use std::{io, time};
use crate::core::core::hash::{Hash, Hashed};
use crate::core::core::pmmr::{self, family, Backend};
use crate::core::core::BlockHeader;
use crate::core::ser::{FixedLength, PMMRable, ProtocolVersion};
use crate::core::ser::{PMMRable, ProtocolVersion};
use crate::leaf_set::LeafSet;
use crate::prune_list::PruneList;
use crate::types::{AppendOnlyFile, DataFile, SizeEntry, SizeInfo};
use croaring::Bitmap;
use std::convert::TryInto;
use std::path::{Path, PathBuf};
const PMMR_HASH_FILE: &str = "pmmr_hash.bin";
@@ -229,11 +230,11 @@ impl<T: PMMRable> Backend<T> for PMMRBackend<T> {
impl<T: PMMRable> PMMRBackend<T> {
/// Instantiates a new PMMR backend.
/// If optional size is provided then treat as "fixed" size otherwise "variable" size backend.
/// Use the provided dir to store its files.
pub fn new<P: AsRef<Path>>(
data_dir: P,
prunable: bool,
fixed_size: bool,
version: ProtocolVersion,
header: Option<&BlockHeader>,
) -> io::Result<PMMRBackend<T>> {
@@ -241,8 +242,8 @@ impl<T: PMMRable> PMMRBackend<T> {
// Are we dealing with "fixed size" data elements or "variable size" data elements
// maintained in an associated size file?
let size_info = if fixed_size {
SizeInfo::FixedSize(T::E::LEN as u16)
let size_info = if let Some(fixed_size) = T::elmt_size() {
SizeInfo::FixedSize(fixed_size)
} else {
SizeInfo::VariableSize(Box::new(AppendOnlyFile::open(
data_dir.join(PMMR_SIZE_FILE),
@@ -252,7 +253,7 @@ impl<T: PMMRable> PMMRBackend<T> {
};
// Hash file is always "fixed size" and we use 32 bytes per hash.
let hash_size_info = SizeInfo::FixedSize(Hash::LEN as u16);
let hash_size_info = SizeInfo::FixedSize(Hash::LEN.try_into().unwrap());
let hash_file = DataFile::open(&data_dir.join(PMMR_HASH_FILE), hash_size_info, version)?;
let data_file = DataFile::open(&data_dir.join(PMMR_DATA_FILE), size_info, version)?;
+4 -4
View File
@@ -16,8 +16,7 @@ use memmap;
use tempfile::tempfile;
use crate::core::ser::{
self, BinWriter, FixedLength, ProtocolVersion, Readable, Reader, StreamingReader, Writeable,
Writer,
self, BinWriter, ProtocolVersion, Readable, Reader, StreamingReader, Writeable, Writer,
};
use std::fmt::Debug;
use std::fs::{self, File, OpenOptions};
@@ -39,8 +38,9 @@ pub struct SizeEntry {
pub size: u16,
}
impl FixedLength for SizeEntry {
const LEN: usize = 8 + 2;
impl SizeEntry {
/// Length of a size entry (8 + 2 bytes) for convenience.
pub const LEN: u16 = 8 + 2;
}
impl Readable for SizeEntry {