-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathstorage_key.rs
More file actions
128 lines (110 loc) · 2.9 KB
/
storage_key.rs
File metadata and controls
128 lines (110 loc) · 2.9 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use unwrap_infallible::UnwrapInfallible;
use crate::{
api::{ErrorApi, ManagedTypeApi},
codec::*,
contract_base::ExitCodecErrorHandler,
types::{heap::BoxedBytes, ManagedBuffer, ManagedByteArray, ManagedType},
*,
};
#[derive(Default)]
pub struct StorageKey<A>
where
A: ManagedTypeApi + ErrorApi + 'static,
{
pub(crate) buffer: ManagedBuffer<A>,
}
impl<A> ManagedType<A> for StorageKey<A>
where
A: ManagedTypeApi + ErrorApi + 'static,
{
type OwnHandle = A::ManagedBufferHandle;
#[inline]
unsafe fn from_handle(handle: A::ManagedBufferHandle) -> Self {
StorageKey {
buffer: ManagedBuffer::from_handle(handle),
}
}
fn get_handle(&self) -> A::ManagedBufferHandle {
self.buffer.get_handle()
}
unsafe fn forget_into_handle(self) -> Self::OwnHandle {
self.buffer.forget_into_handle()
}
fn transmute_from_handle_ref(handle_ref: &A::ManagedBufferHandle) -> &Self {
unsafe { core::mem::transmute(handle_ref) }
}
fn transmute_from_handle_ref_mut(handle_ref: &mut A::ManagedBufferHandle) -> &mut Self {
unsafe { core::mem::transmute(handle_ref) }
}
}
impl<A> StorageKey<A>
where
A: ManagedTypeApi + ErrorApi + 'static,
{
#[inline]
pub fn new(base_key: &[u8]) -> Self {
StorageKey {
buffer: ManagedBuffer::new_from_bytes(base_key),
}
}
#[inline]
pub fn append_bytes(&mut self, bytes: &[u8]) {
self.buffer.append_bytes(bytes);
}
#[inline]
pub fn append_managed_buffer(&mut self, buffer: &ManagedBuffer<A>) {
self.buffer.append(buffer);
}
pub fn append_item<T>(&mut self, item: &T)
where
T: NestedEncode,
{
item.dep_encode_or_handle_err(
&mut self.buffer,
ExitCodecErrorHandler::<A>::from(err_msg::STORAGE_KEY_ENCODE_ERROR),
)
.unwrap_infallible()
}
#[inline]
pub fn to_boxed_bytes(&self) -> BoxedBytes {
self.buffer.to_boxed_bytes()
}
}
impl<M: ManagedTypeApi> From<ManagedBuffer<M>> for StorageKey<M> {
#[inline]
fn from(buffer: ManagedBuffer<M>) -> Self {
StorageKey { buffer }
}
}
impl<M: ManagedTypeApi> From<&str> for StorageKey<M> {
#[inline]
fn from(s: &str) -> Self {
StorageKey {
buffer: ManagedBuffer::from(s),
}
}
}
impl<M, const N: usize> From<ManagedByteArray<M, N>> for StorageKey<M>
where
M: ManagedTypeApi + ErrorApi,
{
#[inline]
fn from(mba: ManagedByteArray<M, N>) -> Self {
StorageKey { buffer: mba.buffer }
}
}
impl<M: ManagedTypeApi> Clone for StorageKey<M> {
fn clone(&self) -> Self {
StorageKey {
buffer: self.buffer.clone(),
}
}
}
impl<A> PartialEq for StorageKey<A>
where
A: ManagedTypeApi + ErrorApi + 'static,
{
fn eq(&self, other: &Self) -> bool {
self.buffer.eq(&other.buffer)
}
}