use std::num::NonZeroUsize;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct BudgetBytes(NonZeroUsize);
impl BudgetBytes {
#[must_use]
pub const fn new(bytes: NonZeroUsize) -> Self { Self(bytes) }
#[must_use]
pub const fn get(self) -> NonZeroUsize { self.0 }
#[must_use]
pub const fn as_usize(self) -> usize { self.0.get() }
}
impl From<NonZeroUsize> for BudgetBytes {
fn from(value: NonZeroUsize) -> Self { Self::new(value) }
}
impl From<BudgetBytes> for NonZeroUsize {
fn from(value: BudgetBytes) -> Self { value.get() }
}
impl From<BudgetBytes> for usize {
fn from(value: BudgetBytes) -> Self { value.as_usize() }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MemoryBudgets {
message_budget: BudgetBytes,
connection_window: BudgetBytes,
assembly_bytes: BudgetBytes,
}
impl MemoryBudgets {
#[must_use]
pub const fn new(
bytes_per_message: BudgetBytes,
bytes_per_connection: BudgetBytes,
bytes_in_flight: BudgetBytes,
) -> Self {
Self {
message_budget: bytes_per_message,
connection_window: bytes_per_connection,
assembly_bytes: bytes_in_flight,
}
}
#[must_use]
pub const fn bytes_per_message(&self) -> BudgetBytes { self.message_budget }
#[must_use]
pub const fn bytes_per_connection(&self) -> BudgetBytes { self.connection_window }
#[must_use]
pub const fn bytes_in_flight(&self) -> BudgetBytes { self.assembly_bytes }
}