#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(
any(feature = "serde", target_arch = "wasm32"),
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
pub struct ChannelId(pub u64);
impl From<u64> for ChannelId {
fn from(value: u64) -> Self {
ChannelId(value)
}
}
impl From<ChannelId> for u64 {
fn from(value: ChannelId) -> Self {
value.0
}
}
impl PartialEq<u64> for ChannelId {
fn eq(&self, other: &u64) -> bool {
self.0 == *other
}
}
pub type WeightedChannel = (ChannelId, u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(
any(feature = "serde", target_arch = "wasm32"),
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
pub struct ReplicaId(pub u64);
impl TryFrom<u64> for ReplicaId {
type Error = crate::Error;
fn try_from(value: u64) -> Result<Self, Self::Error> {
if value == 0 {
return Err(crate::Error::InvalidInput(
"replica_id 0 is reserved as the absent-value sentinel",
));
}
Ok(ReplicaId(value))
}
}
impl From<ReplicaId> for u64 {
fn from(value: ReplicaId) -> Self {
value.0
}
}
impl PartialEq<u64> for ReplicaId {
fn eq(&self, other: &u64) -> bool {
self.0 == *other
}
}
impl std::fmt::Display for ReplicaId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
pub type SharedKey = [u8; 32];
#[cfg(test)]
mod replica_id_tests {
use super::ReplicaId;
#[test]
fn zero_is_rejected() {
assert!(ReplicaId::try_from(0u64).is_err());
}
#[test]
fn non_zero_round_trips() {
let id = ReplicaId::try_from(1002u64).expect("1002 is a valid replica id");
assert_eq!(u64::from(id), 1002);
assert_eq!(id, 1002u64);
}
}