Skip to main content

reifydb_type/value/constraint/
bytes.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::fmt::{self, Display, Formatter};
5
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
9#[repr(transparent)]
10pub struct MaxBytes(u32);
11
12impl MaxBytes {
13	pub fn new(bytes: u32) -> Self {
14		Self(bytes)
15	}
16
17	pub fn value(self) -> u32 {
18		self.0
19	}
20
21	pub const MAX: Self = Self(u32::MAX);
22
23	pub const MIN: Self = Self(0);
24}
25
26impl Display for MaxBytes {
27	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28		self.0.fmt(f)
29	}
30}
31
32impl From<MaxBytes> for u32 {
33	fn from(max_bytes: MaxBytes) -> Self {
34		max_bytes.0
35	}
36}
37
38impl From<u32> for MaxBytes {
39	fn from(value: u32) -> Self {
40		Self::new(value)
41	}
42}
43
44impl From<MaxBytes> for usize {
45	fn from(max_bytes: MaxBytes) -> Self {
46		max_bytes.0 as usize
47	}
48}
49
50impl From<usize> for MaxBytes {
51	fn from(value: usize) -> Self {
52		Self::new(value as u32)
53	}
54}