Skip to main content

sark_core/life/
session.rs

1//! Protocol-independent session lifetime policy.
2
3use std::time::Duration;
4
5use o3::{
6    base::time,
7    buffer::{self, storage},
8};
9
10/// Retained byte ownership accepted by a protocol-independent session.
11pub trait Storage: buffer::raw::Segment + From<storage::Shared> {}
12
13impl<T: buffer::raw::Segment + From<storage::Shared>> Storage for T {}
14
15#[derive(Clone, Copy, PartialEq, Eq)]
16pub struct Config {
17    age: time::Positive,
18    drain: time::Positive,
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum ConfigError {
23    MaxAge { source: time::Zero },
24    DrainTimeout { source: time::Zero },
25}
26
27impl Config {
28    pub const fn new(age: time::Positive, drain: time::Positive) -> Self {
29        Self { age, drain }
30    }
31
32    pub fn try_new(age: Duration, drain: Duration) -> Result<Self, ConfigError> {
33        let age = match time::Positive::try_new(age) {
34            Ok(age) => age,
35            Err(source) => return Err(ConfigError::MaxAge { source }),
36        };
37        let drain = match time::Positive::try_new(drain) {
38            Ok(drain) => drain,
39            Err(source) => return Err(ConfigError::DrainTimeout { source }),
40        };
41        Ok(Self::new(age, drain))
42    }
43
44    pub const fn age(self) -> time::Positive {
45        self.age
46    }
47
48    pub const fn drain(self) -> time::Positive {
49        self.drain
50    }
51}
52
53#[derive(Clone, Copy, Default)]
54pub struct Disabled;
55
56const _: () = {
57    assert!(core::mem::size_of::<Disabled>() == 0);
58};