#[cfg(feature = "std")]
use crate::error::Error;
use crate::error::Result;
use crate::pipeline::Pipeline;
use crate::source::Source;
#[cfg(feature = "std")]
use core::time::Duration;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct RunStats {
pub items_in: u64,
#[cfg(feature = "std")]
pub duration: Duration,
}
pub trait Driver {
fn run<S>(self, pipeline: Pipeline<S>) -> Result<RunStats>
where
S: Source + Send + 'static,
S::Item: Send + 'static,
S::Error: Send + 'static;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct SyncDriver;
impl SyncDriver {
#[must_use]
pub const fn new() -> Self {
Self
}
pub fn run<S>(self, pipeline: Pipeline<S>) -> Result<RunStats>
where
S: Source + 'static,
S::Item: 'static,
S::Error: 'static,
{
crate::pipeline::run_sync(pipeline)
}
}
impl Driver for SyncDriver {
fn run<S>(self, pipeline: Pipeline<S>) -> Result<RunStats>
where
S: Source + Send + 'static,
S::Item: Send + 'static,
S::Error: Send + 'static,
{
crate::pipeline::run_sync(pipeline)
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[derive(Debug, Default, Clone, Copy)]
pub struct ThreadedDriver;
#[cfg(feature = "std")]
impl ThreadedDriver {
#[must_use]
pub const fn new() -> Self {
Self
}
pub fn run<S>(self, pipeline: Pipeline<S>) -> Result<RunStats>
where
S: Source + Send + 'static,
S::Item: Send + 'static,
S::Error: Send + 'static,
{
let handle = std::thread::spawn(move || crate::pipeline::run_sync(pipeline));
match handle.join() {
Ok(result) => result,
Err(_) => Err(Error::Cancelled),
}
}
}
#[cfg(feature = "std")]
impl Driver for ThreadedDriver {
fn run<S>(self, pipeline: Pipeline<S>) -> Result<RunStats>
where
S: Source + Send + 'static,
S::Item: Send + 'static,
S::Error: Send + 'static,
{
Self::run(self, pipeline)
}
}