foundationdb_tuple/
versionstamp.rs

1use super::{Bytes, Element};
2use std::fmt;
3
4#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct Versionstamp {
6    bytes: [u8; 12],
7}
8
9impl fmt::Debug for Versionstamp {
10    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11        Bytes::from(&self.bytes[..]).fmt(f)
12    }
13}
14
15impl Versionstamp {
16    pub fn incomplete(user_version: u16) -> Self {
17        let mut bytes = [0xff; 12];
18        bytes[10..].copy_from_slice(&user_version.to_be_bytes());
19        Versionstamp { bytes }
20    }
21
22    pub fn complete(tr_version: [u8; 10], user_version: u16) -> Self {
23        let mut bytes = [0xff; 12];
24        bytes[0..10].copy_from_slice(&tr_version);
25        bytes[10..].copy_from_slice(&user_version.to_be_bytes());
26        Versionstamp { bytes }
27    }
28
29    pub fn transaction_version(&self) -> &[u8] {
30        &self.bytes[0..10]
31    }
32
33    pub fn user_version(&self) -> u16 {
34        let mut user_version = [0; 2];
35        user_version.copy_from_slice(&self.bytes[10..12]);
36        u16::from_be_bytes(user_version)
37    }
38
39    pub fn is_complete(&self) -> bool {
40        self.bytes[0..10] != [0xff; 10]
41    }
42
43    pub fn as_bytes(&self) -> &[u8; 12] {
44        &self.bytes
45    }
46}
47
48impl From<[u8; 12]> for Versionstamp {
49    fn from(bytes: [u8; 12]) -> Self {
50        Versionstamp { bytes }
51    }
52}
53
54impl Element<'_> {
55    pub fn count_incomplete_versionstamp(&self) -> usize {
56        match self {
57            Element::Versionstamp(v) if !v.is_complete() => 1,
58            Element::Tuple(v) => v.iter().map(Element::count_incomplete_versionstamp).sum(),
59            _ => 0,
60        }
61    }
62}