-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce the special
Pipe
collect_to_vec to use for test
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#[allow(dead_code)] | ||
use super::Pipe; | ||
use crate::errors::{Error, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{ | ||
fmt::Debug, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub(crate) struct Config {} | ||
|
||
type Buffer<I> = Arc<Mutex<Vec<I>>>; | ||
pub struct Processor<I> { | ||
// TODO do we need to use Mutex? | ||
buffer: Buffer<I>, | ||
} | ||
|
||
impl<I> Pipe for Processor<I> { | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
//.lock().unwrap() if mutex | ||
self.buffer.lock().map_err(|err| Error::from(err.to_string()))?.push(input); | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct Collector<I> { | ||
buffer: Buffer<I>, | ||
} | ||
|
||
impl<I> Collector<I> | ||
where | ||
I: Clone, | ||
{ | ||
#[allow(dead_code)] // mainly use in tests | ||
pub fn new() -> Self { | ||
Self { buffer: Arc::new(Mutex::new(vec![])) } | ||
} | ||
|
||
#[allow(dead_code)] // mainly use in tests | ||
pub fn create_pipe(&self) -> Processor<I> { | ||
Processor { buffer: Arc::clone(&self.buffer) } | ||
} | ||
|
||
#[allow(dead_code)] // mainly use in tests | ||
pub fn try_into_iter(&self) -> Result<std::vec::IntoIter<I>> { | ||
self.buffer | ||
.lock() | ||
.map_err(|err| Error::from(err.to_string())) | ||
.map(|buffer| buffer.clone().into_iter()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use crate::errors::Result; | ||
pub mod collect_to_vec; | ||
pub mod discard_all; | ||
pub mod log; | ||
pub mod passthrough; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters