Implemented mock fountain code protocol (#45)
This commit is contained in:
parent
cfcc664e9e
commit
f1412b112e
|
@ -24,3 +24,4 @@ tokio = { version = "1.23", features = ["macros", "rt"] }
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
raptor = ["raptorq"]
|
raptor = ["raptorq"]
|
||||||
|
mock = []
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
// std
|
||||||
|
// crates
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use bytes::Bytes;
|
||||||
|
use futures::{Stream, StreamExt};
|
||||||
|
// internal
|
||||||
|
use crate::fountain::{FountainCode, FountainError};
|
||||||
|
|
||||||
|
/// Fountain code that does no protocol at all.
|
||||||
|
/// Just bypasses the raw bytes into a single chunk and reconstruct from it.
|
||||||
|
pub struct MockFountain;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl FountainCode for MockFountain {
|
||||||
|
type Settings = ();
|
||||||
|
|
||||||
|
fn new(_: Self::Settings) -> Self {
|
||||||
|
Self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode(&self, block: &[u8]) -> Box<dyn Stream<Item = Bytes> + Send + Sync + Unpin> {
|
||||||
|
let data = block.to_vec();
|
||||||
|
Box::new(futures::stream::once(Box::pin(
|
||||||
|
async move { Bytes::from(data) },
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn decode(
|
||||||
|
&self,
|
||||||
|
mut stream: impl Stream<Item = Bytes> + Send + Sync + Unpin,
|
||||||
|
) -> Result<Bytes, FountainError> {
|
||||||
|
if let Some(chunk) = stream.next().await {
|
||||||
|
Ok(chunk)
|
||||||
|
} else {
|
||||||
|
Err("Stream ended before decoding was complete".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
#[cfg(feature = "mock")]
|
||||||
|
pub mod mock;
|
||||||
#[cfg(feature = "raptor")]
|
#[cfg(feature = "raptor")]
|
||||||
pub mod raptorq;
|
pub mod raptorq;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue