-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathcommon.rs
More file actions
95 lines (76 loc) · 2.08 KB
/
Copy pathcommon.rs
File metadata and controls
95 lines (76 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2023 - 2026 Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
use bilrost::{
DecodeErrorKind,
encoding::{EmptyState, ForOverwrite, Proxiable},
};
use restate_encoding_derive::BilrostNewType;
use restate_platform::network::NetSerde;
use crate::bilrost_encodings::RestateEncoding;
struct U128Tag;
impl Proxiable<U128Tag> for u128 {
type Proxy = (u64, u64);
fn encode_proxy(&self) -> Self::Proxy {
((*self >> 64) as u64, *self as u64)
}
fn decode_proxy(&mut self, proxy: Self::Proxy) -> Result<(), DecodeErrorKind> {
*self = (proxy.0 as u128) << 64 | proxy.1 as u128;
Ok(())
}
}
impl ForOverwrite<RestateEncoding, u128> for () {
fn for_overwrite() -> u128 {
0
}
}
impl EmptyState<RestateEncoding, u128> for () {
fn empty() -> u128 {
0
}
fn is_empty(val: &u128) -> bool {
*val == 0
}
fn clear(val: &mut u128) {
*val = 0;
}
}
bilrost::delegate_proxied_encoding!(
use encoding (::bilrost::encoding::General)
to encode proxied type (u128)
using proxy tag (U128Tag)
with encoding (RestateEncoding)
);
/// A Bilrost compatible U128 type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, BilrostNewType)]
pub struct U128((u64, u64));
impl From<u128> for U128 {
fn from(value: u128) -> Self {
Self(((value >> 64) as u64, value as u64))
}
}
impl From<U128> for u128 {
fn from(value: U128) -> Self {
(value.0.0 as u128) << 64 | value.0.1 as u128
}
}
impl NetSerde for U128 {}
#[cfg(test)]
mod test {
use rand::random;
use super::U128;
#[test]
fn test_u128() {
(0..100).for_each(|_| {
let num = random::<u128>();
let value = U128::from(num);
assert_eq!(num, u128::from(value));
});
}
}