rustzmq2 0.1.0

A native async Rust implementation of ZeroMQ
Documentation
//! Per-socket façade bookkeeping shared across every kind: a typed backend
//! handle, the table of accepted-bind stop handles, and the resolved
//! last-bound endpoint.
//!
//! The `Socket` trait provides default method bodies that delegate through
//! the `HasCommon` accessor trait, so individual kinds only need to store a
//! `SocketCommon<Self::Backend>` field and implement `HasCommon`. They do
//! not need to re-implement `backend`, `binds`, `set_last_endpoint`, or
//! `last_endpoint` themselves.
//!
//! Reconnect-handle storage is *not* part of `SocketCommon` because its
//! shape varies per kind (`SocketCore` uses a `HashMap<Endpoint, …>`;
//! `SubSocket` uses a `Vec<…>`). `connect` therefore stays a required
//! trait method.

use super::backend::MultiPeerBackend;
use crate::endpoint::Endpoint;
use crate::transport::AcceptStopHandle;

use std::collections::HashMap;
#[cfg(feature = "inproc")]
use std::collections::HashSet;
use std::sync::Arc;

#[doc(hidden)]
pub struct SocketCommon<B: MultiPeerBackend + 'static> {
    pub backend: Arc<B>,
    pub binds: HashMap<Endpoint, AcceptStopHandle>,
    pub last_endpoint: Option<Endpoint>,
    /// Set of option names we've already warned about as ignored on inproc.
    /// De-dupes the `log::warn!` + `OptionIgnoredOnTransport` monitor event
    /// across multiple inproc binds/connects of the same socket.
    #[cfg(feature = "inproc")]
    pub ignored_warned: HashSet<&'static str>,
}

impl<B: MultiPeerBackend + 'static> SocketCommon<B> {
    pub(crate) fn new(backend: Arc<B>) -> Self {
        Self {
            backend,
            binds: HashMap::new(),
            last_endpoint: None,
            #[cfg(feature = "inproc")]
            ignored_warned: HashSet::new(),
        }
    }
}

/// Accessor for `SocketCommon`. Every `Socket` impl exposes its
/// bookkeeping through this trait so the `Socket` trait can provide the
/// `backend` / `binds` / `set_last_endpoint` / `last_endpoint` defaults.
///
/// Hidden from rustdoc: an implementation detail of the `Socket` trait,
/// not part of the stable API surface. `Socket` is `#[doc(hidden)]` on
/// this supertrait bound.
#[doc(hidden)]
pub trait HasCommon {
    type Backend: MultiPeerBackend + 'static;
    fn common(&self) -> &SocketCommon<Self::Backend>;
    fn common_mut(&mut self) -> &mut SocketCommon<Self::Backend>;
}