Skip to main content

reifydb_value/
count.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::{
5	fmt::{self, Display, Formatter},
6	ops::Add,
7};
8
9use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
12#[repr(transparent)]
13pub struct Count(u64);
14
15impl Count {
16	pub const ZERO: Self = Self(0);
17
18	pub const fn new(count: u64) -> Self {
19		Self(count)
20	}
21
22	pub const fn as_u64(self) -> u64 {
23		self.0
24	}
25
26	pub const fn saturating_add(self, other: Self) -> Self {
27		Self(self.0.saturating_add(other.0))
28	}
29
30	pub const fn saturating_sub(self, other: Self) -> Self {
31		Self(self.0.saturating_sub(other.0))
32	}
33}
34
35impl Add for Count {
36	type Output = Self;
37
38	fn add(self, rhs: Self) -> Self {
39		Self(self.0 + rhs.0)
40	}
41}
42
43impl Display for Count {
44	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45		write!(f, "{}", self.0)
46	}
47}
48
49impl From<Count> for u64 {
50	fn from(count: Count) -> Self {
51		count.0
52	}
53}