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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Various functions and data structures to organize and work with raw stable memory.
//!
//! This crate provides a notion of "stable memory ownership". This is a set of techniques, that
//! make data stored in stable memory appear like it gets automatically garbage collected by Rust's borrower.
//! Each stable memory primitive includes a `stable drop flag` - a special flag, that is used by
//! Rust to understand, whether it should release stable memory, when it naturally drops the value,
//! or not.
//!
//! Stable drop flag rules are simple:
//! 1. When you write something to stable memory, set its drop flag to `off` position.
//! 2. When you read something from stable memory, and you don't have an intention to move it,
//! set the drop flag to `off` position.
//! 3. When you read something from stable memory with an intention to move this data somewhere else,
//! set the drop flag to `on` position.
//! 4. When you [Drop] the value, if the drop flag is `on` - call [StableType::stable_drop](crate::StableType::stable_drop),
//! otherwise just [Drop].
//!
//! These rules are transparently managed at runtime. For users of this crate it appears like
//! stable memory can "own" some value. A set of lifetimes applied later, on the data structure layer,
//! to make it seem like the value is owned by a particular stable data structure.
//!
//! If you're thinking of implementing your own data structure using this crate, check [this](https://github.com/seniorjoinu/ic-stable-memory/docs/user-defined-data-structures.md)
//! document for more info on this topic.
use crate;
use crateStableType;
use cratestable;
/// A pointer to something is stable memory.
///
/// Just a handy alias for [u64].
pub type StablePtr = u64;
pub type StablePtrBuf = Buf;
pub
/// Reads raw bytes from stable memory.
///
/// Under the hood simply calls [stable64_read](ic_cdk::api::stable::stable64_read).
///
/// # Safety
/// Make sure you're reading from a valid memory block. All kinds of bad things can happen.
/// Also, this function does not handle stable memory `ownership` in any way, so you have to make sure
/// your data won't get stable-dropped manually. See [crate::SBox] for an example of how this can be done.
pub unsafe
/// Write raw bytes to stable memory.
///
/// Under the hood simply calls [stable64_write](ic_cdk::api::stable::stable64_write).
///
/// # Safety
/// Make sure you're writing to a valid memory block. All kinds of bad things can happen.
/// Also, this function does not handle stable memory `ownership` in any way, so you have to make sure
/// your data won't get stable-dropped manually. See [SBox](crate::SBox) for an example of how this can be done.
pub unsafe
/// Reads a [StableType](crate::StableType) value *that won't move* implementing [AsFixedSizeBytes](crate::AsFixedSizeBytes) trait from stable memory.
///
/// See also [read_fixed_for_move].
///
/// This function creates an intermediate buffer of [AsFixedSizeBytes::SIZE](crate::AsFixedSizeBytes::SIZE) bytes,
/// reads bytes from stable memory into it, then deserializes this buffer into a value itself and
/// then sets its stable drop flag to `off` position.
///
/// # Safety
/// Make sure you're reading from a valid memory block. All kinds of bad things can happen.
/// This function manages stable memory `ownership`, this value *won't* be stable-dropped after it goes
/// out of scope. Make sure you treat it accordingly.
pub unsafe
/// Reads a [StableType](crate::StableType) value *that will move* implementing [AsFixedSizeBytes](crate::AsFixedSizeBytes) trait from stable memory.
///
/// See also [read_fixed_for_reference].
///
/// This function creates an intermediate buffer of [AsFixedSizeBytes::SIZE](crate::AsFixedSizeBytes::SIZE) bytes,
/// reads bytes from stable memory into it, then deserializes this buffer into a value itself and
/// then sets its stable drop flag to `on` position.
///
/// # Safety
/// Make sure you're reading from a valid memory block. All kinds of bad things can happen.
/// This function manages stable memory `ownership`, this value *will* be stable-dropped after it goes
/// out of scope. Make sure you treat it accordingly.
pub unsafe
/// Writes a [StableType](crate::StableType) value implementing [AsFixedSizeBytes](crate::AsFixedSizeBytes) trait to stable memory.
///
/// This function creates an intermediate buffer of [AsFixedSizeBytes::SIZE](crate::AsFixedSizeBytes::SIZE) bytes,
/// serializes the value into that buffer, writes the buffer into stable memory and then sets the stable
/// drop flag of the value to `off` position.
///
/// # Safety
/// Make sure you're writing to a valid memory block. All kinds of bad things can happen.
/// This function manages stable memory `ownership`, this value *won't* be stable-dropped after it goes
/// out of scope. Make sure you treat it accordingly.
pub unsafe
/// Wipes out stable memory, making it zero pages again.
///
/// Utility function which is only available for targets other than `wasm`. Useful for tests.
///
/// # Safety
/// Make sure to drop all previously created stable structures and reinit the allocator.
pub unsafe