use std::fmt;
use std::sync::Arc;
use crate::runtime::ClientRuntime;
#[derive(Clone)]
pub struct ClientSession {
runtime: Arc<ClientRuntime>,
}
impl ClientSession {
#[must_use]
pub fn new(runtime: Arc<ClientRuntime>) -> Self {
Self { runtime }
}
#[must_use]
pub fn runtime(&self) -> Arc<ClientRuntime> {
Arc::clone(&self.runtime)
}
#[must_use]
pub fn shares_runtime_with(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.runtime, &other.runtime)
}
}
impl fmt::Debug for ClientSession {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("ClientSession")
.field("runtime", &"<shared>")
.finish()
}
}
pub trait ClientSessionProvider {
#[must_use]
fn client_session(&self) -> Option<&ClientSession>;
}
#[cfg(test)]
mod tests {
use super::ClientSession;
use crate::runtime::test_client_runtime;
#[test]
fn cloning_session_preserves_runtime_identity() {
let session = ClientSession::new(test_client_runtime("client-session-identity"));
let clone = session.clone();
assert!(session.shares_runtime_with(&clone));
}
}