From f695c2785b948541e5f7d185c21fc54c74581cef Mon Sep 17 00:00:00 2001 From: Evaldas Buinauskas <7301441+buinauskas@users.noreply.github.com> Date: Sat, 8 Oct 2022 13:50:12 +0300 Subject: [PATCH] Clean relays up (#27) * Flatten * Cleanup noop relay --- src/{relay => }/http.rs | 0 src/lib.rs | 54 ++++++++++++++++++++++++++++------------- src/relay/mod.rs | 18 -------------- src/relay/noop.rs | 16 ------------ src/{relay => }/udp.rs | 0 5 files changed, 37 insertions(+), 51 deletions(-) rename src/{relay => }/http.rs (100%) delete mode 100644 src/relay/mod.rs delete mode 100644 src/relay/noop.rs rename src/{relay => }/udp.rs (100%) diff --git a/src/relay/http.rs b/src/http.rs similarity index 100% rename from src/relay/http.rs rename to src/http.rs diff --git a/src/lib.rs b/src/lib.rs index dfa70e4..fcae4af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,18 +41,21 @@ mod error; mod event; -pub mod relay; + +mod http; +mod udp; pub use self::error::*; pub use self::event::*; -pub use self::relay::*; +pub use self::http::*; +pub use self::udp::*; use std::{ hint::spin_loop, sync::atomic::{AtomicUsize, Ordering}, }; -static mut RELAY: &dyn Relay = &Noop; +static mut RELAY: &dyn Relay = &NopRelay; static STATE: AtomicUsize = AtomicUsize::new(0); const UNINITIALIZED: usize = 0; @@ -91,30 +94,18 @@ where fn relay() -> &'static dyn Relay { if STATE.load(Ordering::SeqCst) != INITIALIZED { - static NOP: Noop = Noop; + static NOP: NopRelay = NopRelay; &NOP } else { unsafe { RELAY } } } -/// Initializes [`Relay`] for the whole application +/// Sets the global [Relay] pub fn set_relay(relay: T) -> Result<(), SetRelayError> { set_relay_inner(|| Box::leak(Box::new(relay))) } -/// Tracks the actual event -pub fn track(event: Event) -> Result<(), Error> -where - T: std::fmt::Debug + serde::Serialize, -{ - let event_vec = serde_json::to_vec(&event)?; - - relay().transport(event.base, event_vec); - - Ok(()) -} - /// The type returned by [`set_relay`] if [`set_relay`] has already been called. pub struct SetRelayError(()); @@ -131,3 +122,32 @@ impl std::fmt::Display for SetRelayError { } impl std::error::Error for SetRelayError {} + +/// Tracks the actual event +pub fn track(event: Event) -> Result<(), Error> +where + T: std::fmt::Debug + serde::Serialize, +{ + let event_vec = serde_json::to_vec(&event)?; + + relay().transport(event.base, event_vec); + + Ok(()) +} + +/// Trait for event transportation +pub trait Relay { + /// Accepts event, serialized in JSON, in a form of bytes. + /// + /// Use these bytes to send the event over the wire using protocols, such as: + /// - HTTP + /// - TCP + /// - UDP + fn transport(&self, event_base: EventBase, event: Vec); +} + +struct NopRelay; + +impl Relay for NopRelay { + fn transport(&self, _: EventBase, _: Vec) {} +} diff --git a/src/relay/mod.rs b/src/relay/mod.rs deleted file mode 100644 index 20c8252..0000000 --- a/src/relay/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -//! [`Relay`] is an abstraction on where events will be sent to - -mod http; -mod noop; -mod udp; - -pub use self::http::*; -pub use self::noop::*; -pub use self::udp::*; - -/// Trait for event transportation -pub trait Relay { - /// Accepts serialized event as bytes that should be sent to a certain protocol, such as: - /// - HTTP - /// - TCP - /// - UDP - fn transport(&self, event_base: crate::EventBase, event: Vec); -} diff --git a/src/relay/noop.rs b/src/relay/noop.rs deleted file mode 100644 index 5ebef32..0000000 --- a/src/relay/noop.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::{EventBase, Relay}; - -/// A [`Relay`] that won't do anything with events -#[derive(Debug, Default, Clone, Copy)] -pub struct Noop; - -impl Noop { - /// Creates an instance of [`Noop`] [`Relay`] - pub fn new() -> Self { - Self - } -} - -impl Relay for Noop { - fn transport(&self, _event_base: EventBase, _event: Vec) {} -} diff --git a/src/relay/udp.rs b/src/udp.rs similarity index 100% rename from src/relay/udp.rs rename to src/udp.rs