//! IDs used for data objects and models. //! //! UUID is always unique; the numeric ID used as a fallback //! is auto-incrementing, but some values may be skipped. //! //! It is better to treat both ID implementations as opaque. #[cfg(feature = "uuid-ids")] mod impl_uuid { /// Common identifier type #[allow(non_camel_case_types)] pub type ID = uuid::Uuid; pub fn next_id() -> ID { uuid::Uuid::new_v4() } pub fn zero_id() -> ID { uuid::Uuid::nil() } } #[cfg(not(feature = "uuid-ids"))] mod impl_u64 { /// Common identifier type #[allow(non_camel_case_types)] pub type ID = u64; lazy_static::lazy_static! { static ref COUNTER: parking_lot::Mutex = parking_lot::Mutex::new(0); } pub fn next_id() -> ID { let mut m = COUNTER.lock(); let v = *m; *m += 1; v } pub fn zero_id() -> ID { 0 } } #[cfg(feature = "uuid-ids")] pub use impl_uuid::{ID, next_id, zero_id}; #[cfg(not(feature = "uuid-ids"))] pub use impl_u64::{ID, next_id, zero_id}; /// Something that has ID pub trait HaveId { fn get_id(&self) -> ID; }