use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_IDENTITY: AtomicU64 = AtomicU64::new(1);
pub(crate) fn next_identity() -> NonZeroU64 {
let raw = NEXT_IDENTITY.fetch_add(1, Ordering::Relaxed);
NonZeroU64::new(raw).expect("identity counter starts at 1 and only increases")
}
macro_rules! identity_newtype {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name(NonZeroU64);
impl $name {
pub fn fresh() -> Self {
Self($crate::lifecycle_identity::next_identity())
}
#[allow(dead_code)]
pub(crate) fn raw_for_render(&self) -> u64 {
self.0.get()
}
}
};
}
identity_newtype!(
BindingIdentity
);
identity_newtype!(
ObserverToken
);
identity_newtype!(
CandidateIdentity
);
identity_newtype!(
GenerationIdentity
);
identity_newtype!(
PublicationIdentity
);
identity_newtype!(
SnapshotIdentity
);
identity_newtype!(
AuthorityIdentity
);
identity_newtype!(
ProvenanceIdentity
);
identity_newtype!(
OperationIdentity
);
identity_newtype!(
EvaluationIdentity
);
identity_newtype!(
WorktreeScanId
);
identity_newtype!(
ProducingRuntimeIdentity
);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OperationKind {
AcquireRuntime,
CloseSource,
OpenEmbeddedSource,
RefreshSource,
SearchSymbols,
SearchText,
ShutdownRuntime,
IndexCensus,
SearchKnowledge,
}
impl OperationKind {
pub const ALL: [Self; 9] = [
Self::AcquireRuntime,
Self::CloseSource,
Self::OpenEmbeddedSource,
Self::RefreshSource,
Self::SearchSymbols,
Self::SearchText,
Self::ShutdownRuntime,
Self::IndexCensus,
Self::SearchKnowledge,
];
pub fn kind_name(self) -> &'static str {
match self {
Self::AcquireRuntime => "AcquireRuntime",
Self::CloseSource => "CloseSource",
Self::OpenEmbeddedSource => "OpenEmbeddedSource",
Self::RefreshSource => "RefreshSource",
Self::SearchSymbols => "SearchSymbols",
Self::SearchText => "SearchText",
Self::ShutdownRuntime => "ShutdownRuntime",
Self::IndexCensus => "IndexCensus",
Self::SearchKnowledge => "SearchKnowledge",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CanonicalArgumentHash(u64);
impl CanonicalArgumentHash {
pub fn of_normalized(bytes: &[u8]) -> Self {
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x1000_0000_01b3);
}
Self(hash)
}
pub fn raw(self) -> u64 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OperationReceipt {
identity: OperationIdentity,
operation_kind: OperationKind,
schema_version: u32,
canonical_argument_hash: CanonicalArgumentHash,
}
impl OperationReceipt {
pub const SCHEMA_VERSION: u32 = 1;
pub fn normalized(operation_kind: OperationKind, normalized_arguments: &[u8]) -> Self {
Self {
identity: OperationIdentity::fresh(),
operation_kind,
schema_version: Self::SCHEMA_VERSION,
canonical_argument_hash: CanonicalArgumentHash::of_normalized(normalized_arguments),
}
}
#[cfg(any(test, feature = "__test-internals"))]
pub fn for_test(operation_kind: OperationKind) -> Self {
Self::for_dark_refusal(operation_kind)
}
pub(crate) fn for_dark_refusal(operation_kind: OperationKind) -> Self {
Self::normalized(operation_kind, operation_kind.kind_name().as_bytes())
}
pub fn identity(&self) -> OperationIdentity {
self.identity
}
pub fn operation_kind(&self) -> OperationKind {
self.operation_kind
}
pub fn schema_version(&self) -> u32 {
self.schema_version
}
pub fn canonical_argument_hash(&self) -> CanonicalArgumentHash {
self.canonical_argument_hash
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SourceRefusalKind {
AdmissionUnavailable,
InvalidSelection,
SelectionUnavailable,
SourceUnavailable,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RetryAdvice {
Automatic,
Never,
OnEvent,
Operator,
}
impl RetryAdvice {
pub const ALL: [Self; 4] = [Self::Automatic, Self::Never, Self::OnEvent, Self::Operator];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceRefusal {
kind: SourceRefusalKind,
operation: OperationReceipt,
retry: RetryAdvice,
evidence_identity: Option<AuthorityIdentity>,
}
impl SourceRefusal {
pub(crate) fn new(
kind: SourceRefusalKind,
operation: OperationReceipt,
retry: RetryAdvice,
evidence_identity: Option<AuthorityIdentity>,
) -> Self {
Self {
kind,
operation,
retry,
evidence_identity,
}
}
pub(crate) fn for_runtime(
kind: SourceRefusalKind,
operation: OperationReceipt,
retry: RetryAdvice,
evidence_identity: Option<AuthorityIdentity>,
) -> Self {
Self::new(kind, operation, retry, evidence_identity)
}
pub fn kind(&self) -> SourceRefusalKind {
self.kind
}
pub fn operation(&self) -> OperationReceipt {
self.operation
}
pub fn retry(&self) -> RetryAdvice {
self.retry
}
pub fn evidence_identity(&self) -> Option<AuthorityIdentity> {
self.evidence_identity
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenerationAuthority {
identity: AuthorityIdentity,
generation: GenerationIdentity,
physical_root: String,
}
impl GenerationAuthority {
pub(crate) fn captured(
identity: AuthorityIdentity,
generation: GenerationIdentity,
physical_root: String,
) -> Self {
Self {
identity,
generation,
physical_root,
}
}
pub fn identity(&self) -> AuthorityIdentity {
self.identity
}
pub fn generation(&self) -> GenerationIdentity {
self.generation
}
pub fn physical_root(&self) -> &str {
&self.physical_root
}
pub fn proves_generation_absence(&self) -> bool {
true
}
pub fn proves_repository_absence(&self) -> bool {
false
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObservationTime(NonZeroU64);
impl ObservationTime {
pub fn fresh() -> Self {
Self(next_identity())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObserverEpoch(u64);
impl ObserverEpoch {
pub const fn initial() -> Self {
Self(0)
}
pub const fn advanced(self) -> Self {
Self(self.0 + 1)
}
pub const fn raw(self) -> u64 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InvalidationSequence(u64);
impl InvalidationSequence {
pub const fn initial() -> Self {
Self(0)
}
pub const fn advanced(self) -> Self {
Self(self.0 + 1)
}
pub const fn raw(self) -> u64 {
self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_counter_serves_every_identity_kind() {
let a = GenerationIdentity::fresh().0;
let b = AuthorityIdentity::fresh().0;
let c = OperationIdentity::fresh().0;
let d = ObservationTime::fresh().0;
let mut raws = vec![a, b, c, d];
raws.sort_unstable();
raws.dedup();
assert_eq!(raws.len(), 4, "one shared counter must not repeat a value");
}
#[test]
fn a_fresh_identity_never_repeats_within_its_own_kind() {
let first = GenerationIdentity::fresh();
let second = GenerationIdentity::fresh();
assert_ne!(first, second);
}
#[test]
fn monotonic_positions_never_rewind() {
let epoch = ObserverEpoch::initial();
assert!(epoch.advanced() > epoch);
let sequence = InvalidationSequence::initial();
assert!(sequence.advanced() > sequence);
}
}