From 6aa033ebbb4594357c6ff0e8c61176f18e6c0108 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 15 Oct 2020 09:46:24 +0100 Subject: [PATCH] ddc contain --- src/container.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/dedup.rs | 4 ++-- src/handle.rs | 2 +- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/container.rs b/src/container.rs index 158ddef..1da711b 100644 --- a/src/container.rs +++ b/src/container.rs @@ -1,9 +1,55 @@ //! Container for chain use super::*; use chain::{Chain, Chainable}; +use dedup::HashRefSet; +#[derive(Debug, Serialize, Deserialize)] pub struct ChainContainer { chain: Chain, - inputs_exact: dedup::HashRefSet<[T]>, + inputs_exact: HashRefSet<[T]>, +} + +impl ChainContainer +{ + pub fn new() -> Self + { + Self { + chain: Chain::new(), + inputs_exact: HashRefSet::new(), + } + } + + pub fn force_feed>(&mut self, tokens: U) + { + self.inputs_exact.insert(&tokens.as_ref()); + self.chain.feed(tokens); + } + pub fn feed>(&mut self, tokens: U) -> bool + { + let tokens=tokens.as_ref(); + if self.inputs_exact.insert(&tokens) { + self.chain.feed(tokens); + true + } else { + trace!("Exact duplication input disallowed"); + false + } + } + + pub fn chain(&self) -> &Chain + { + &self.chain + } +} + +impl From> for ChainContainer +{ + fn from(chain: Chain) -> Self + { + Self { + chain, + inputs_exact: HashRefSet::new(), + } + } } diff --git a/src/dedup.rs b/src/dedup.rs index c74331b..fd00c60 100644 --- a/src/dedup.rs +++ b/src/dedup.rs @@ -9,7 +9,7 @@ use std::{ use sha2::{Sha256, Digest}; use cryptohelpers::sha256::Sha256Hash; -fn compute(thing: &T) -> Sha256Hash +fn compute(thing: &T) -> Sha256Hash { use std::mem::size_of; struct Sha256Hasher(Sha256); @@ -65,7 +65,7 @@ impl util::NewCapacity for HashRefSet } } -impl HashRefSet +impl HashRefSet { pub fn insert(&mut self, value: &U) -> bool where U: Borrow diff --git a/src/handle.rs b/src/handle.rs index 10590c6..6f42cc9 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -245,7 +245,7 @@ pub async fn host(from: ChainHandle) trace!("Begin polling on child"); tokio::select!{ v = &mut child => { - match v { + match v { #[cold] Ok(_) => {warn!("Child exited before we have? This should probably never happen.")},//Should never happen. Err(e) => {error!("Child exited abnormally. Aborting: {}", e)}, //Child panic or cancel. }