diff --git a/network-hypervisor/src/vl1/node.rs b/network-hypervisor/src/vl1/node.rs index a32f397c4..570c1b239 100644 --- a/network-hypervisor/src/vl1/node.rs +++ b/network-hypervisor/src/vl1/node.rs @@ -24,8 +24,8 @@ use zerotier_utils::error::InvalidParameterError; use zerotier_utils::gate::IntervalGate; use zerotier_utils::hex; use zerotier_utils::marshalable::Marshalable; -use zerotier_utils::pocket::Pocket; use zerotier_utils::ringbuffer::RingBuffer; +use zerotier_utils::thing::Thing; /// Trait implemented by external code to handle events and provide an interface to the system or application. /// @@ -248,7 +248,7 @@ pub struct Node { intervals: Mutex, /// Canonicalized network paths, held as Weak<> to be automatically cleaned when no longer in use. - paths: RwLock>, + paths: RwLock>, /// Peers with which we are currently communicating. peers: RwLock>>, @@ -300,7 +300,7 @@ impl Node { instance_id: random::get_bytes_secure(), identity: id, intervals: Mutex::new(BackgroundTaskIntervals::default()), - paths: RwLock::new(Pocket::new(PathMap::::new())), + paths: RwLock::new(Thing::new(PathMap::::new())), peers: RwLock::new(HashMap::new()), roots: RwLock::new(RootInfo { sets: HashMap::new(), diff --git a/network-hypervisor/src/vl1/path.rs b/network-hypervisor/src/vl1/path.rs index a1d8ed599..2f917e3c8 100644 --- a/network-hypervisor/src/vl1/path.rs +++ b/network-hypervisor/src/vl1/path.rs @@ -10,7 +10,7 @@ use crate::vl1::endpoint::Endpoint; use crate::vl1::node::*; use zerotier_crypto::random; -use zerotier_utils::pocket::Pocket; +use zerotier_utils::thing::Thing; use zerotier_utils::NEVER_HAPPENED_TICKS; pub(crate) const SERVICE_INTERVAL_MS: i64 = protocol::PATH_KEEPALIVE_INTERVAL; @@ -27,8 +27,8 @@ pub(crate) enum PathServiceResult { /// for them and uniform application of things like keepalives. pub struct Path { pub endpoint: Endpoint, - local_socket: Pocket<64>, - local_interface: Pocket<64>, + local_socket: Thing<64>, + local_interface: Thing<64>, last_send_time_ticks: AtomicI64, last_receive_time_ticks: AtomicI64, create_time_ticks: i64, @@ -44,8 +44,8 @@ impl Path { ) -> Self { Self { endpoint, - local_socket: Pocket::new(local_socket), // enlarge Pocket<> if this panics - local_interface: Pocket::new(local_interface), // enlarge Pocket<> if this panics + local_socket: Thing::new(local_socket), // enlarge Thing<> if this panics + local_interface: Thing::new(local_interface), // enlarge Thing<> if this panics last_send_time_ticks: AtomicI64::new(NEVER_HAPPENED_TICKS), last_receive_time_ticks: AtomicI64::new(NEVER_HAPPENED_TICKS), create_time_ticks: time_ticks, diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 4e4d5313d..99a49073c 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -14,10 +14,10 @@ pub mod io; pub mod json; pub mod marshalable; pub mod memory; -pub mod pocket; pub mod pool; pub mod ringbuffer; pub mod ringbuffermap; +pub mod thing; pub mod varint; #[cfg(feature = "tokio")] diff --git a/utils/src/pocket.rs b/utils/src/thing.rs similarity index 80% rename from utils/src/pocket.rs rename to utils/src/thing.rs index d4c6914eb..9f8445ee1 100644 --- a/utils/src/pocket.rs +++ b/utils/src/thing.rs @@ -4,22 +4,22 @@ use std::any::TypeId; use std::mem::{forget, size_of, MaybeUninit}; use std::ptr::{drop_in_place, read, write}; -/// A statically sized container that acts a bit like Box but with less overhead. +/// A statically sized container that acts like Box but without heap overhead. /// -/// This is used in a few places to avoid cascades of templates by allowing templated -/// objects to be held generically and accessed only within templated functions. There's a -/// bit of unsafe here but externally it's safe and panics if misused. +/// This is used in a couple places to avoid what would otherwise be an explosion of cascading +/// template parameters to support externally defined small objects. It does so with virtually +/// no overhead, unlike Box<>, and could be no_std compatible. /// /// This will panic if the capacity is too small. If that occurs, it must be enlarged. It will /// also panic if any of the accessors (other than the try_ versions) are used to try to get /// a type other than the one it was constructed with. -pub struct Pocket { +pub struct Thing { storage: [u8; CAPACITY], dropper: fn(*mut u8), data_type: TypeId, } -impl Pocket { +impl Thing { #[inline(always)] pub fn new(x: T) -> Self { assert!(size_of::() <= CAPACITY); @@ -73,7 +73,7 @@ impl Pocket { } } -impl AsRef for Pocket { +impl AsRef for Thing { #[inline(always)] fn as_ref(&self) -> &T { assert_eq!(TypeId::of::(), self.data_type); @@ -81,7 +81,7 @@ impl AsRef for Pocket { } } -impl AsMut for Pocket { +impl AsMut for Thing { #[inline(always)] fn as_mut(&mut self) -> &mut T { assert_eq!(TypeId::of::(), self.data_type); @@ -89,7 +89,7 @@ impl AsMut for Pocket { } } -impl Drop for Pocket { +impl Drop for Thing { #[inline(always)] fn drop(&mut self) { (self.dropper)((self as *mut Self).cast()); @@ -105,9 +105,9 @@ mod tests { fn typing_and_life_cycle() { let test_obj = Rc::new(1i32); assert_eq!(Rc::strong_count(&test_obj), 1); - let a = Pocket::<32>::new(test_obj.clone()); - let b = Pocket::<32>::new(test_obj.clone()); - let c = Pocket::<32>::new(test_obj.clone()); + let a = Thing::<32>::new(test_obj.clone()); + let b = Thing::<32>::new(test_obj.clone()); + let c = Thing::<32>::new(test_obj.clone()); assert!(a.get::>().eq(b.get())); assert!(a.try_get::>().is_some()); assert!(a.try_get::>().is_none());