use std::sync::Arc;
use std::time::Duration;
use crate::{ClientOptions, Envelope};
pub trait Transport: Send + Sync + 'static {
fn send_envelope(&self, envelope: Envelope);
fn flush(&self, timeout: Duration) -> bool {
let _timeout = timeout;
true
}
fn shutdown(&self, timeout: Duration) -> bool {
self.flush(timeout)
}
}
pub trait TransportFactory: Send + Sync {
fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport>;
}
impl<F> TransportFactory for F
where
F: Fn(&ClientOptions) -> Arc<dyn Transport> + Clone + Send + Sync + 'static,
{
fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
(*self)(options)
}
}
impl<T: Transport> Transport for Arc<T> {
fn send_envelope(&self, envelope: Envelope) {
(**self).send_envelope(envelope)
}
fn shutdown(&self, timeout: Duration) -> bool {
(**self).shutdown(timeout)
}
}
impl<T: Transport> TransportFactory for Arc<T> {
fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
let _options = options;
self.clone()
}
}