collab-common 0.0.7

Code shared by collab's client and server
Documentation
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use serde::{Deserialize, Serialize};

/// An identifier for a file that remains stable across moves and renames.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct FileId(u64);

impl FileId {
    #[inline(always)]
    pub(crate) fn new(id: u64) -> Self {
        Self(id)
    }
}

/// An identifier for a file that remains stable across moves and renames.
#[derive(Default, Clone, Debug)]
pub struct FileIdGenerator(Arc<AtomicU64>);

impl PartialEq<Self> for FileIdGenerator {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for FileIdGenerator {}

impl FileIdGenerator {
    /// TODO: docs
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }

    /// TODO: docs
    #[inline(always)]
    pub fn next(&self) -> FileId {
        FileId::new(self.0.fetch_add(1, Ordering::Relaxed))
    }
}