rune/modules/
mem.rs

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Working with memory.

use crate as rune;
use crate::alloc::fmt::TryWrite;
use crate::runtime::{self, Formatter, Value, VmResult};
use crate::{Any, ContextError, Module};

/// Working with memory.
#[rune::module(::std::mem)]
pub fn module() -> Result<Module, ContextError> {
    let mut m = Module::from_meta(self::module_meta)?;
    m.function_meta(drop)?;
    m.function_meta(snapshot)?;

    m.ty::<Snapshot>()?;
    m.function_meta(Snapshot::display)?;
    m.function_meta(Snapshot::debug)?;
    m.function_meta(Snapshot::shared)?;
    m.function_meta(Snapshot::is_exclusive)?;
    m.function_meta(Snapshot::is_readable)?;
    m.function_meta(Snapshot::is_writable)?;

    Ok(m)
}

#[derive(Any)]
#[rune(item = ::std::mem)]
struct Snapshot {
    inner: runtime::Snapshot,
}

impl Snapshot {
    /// The number of shared references to the value.
    ///
    /// # Examples
    ///
    /// ```rune
    /// use std::mem::snapshot;
    ///
    /// let v = [1, 2, 3];
    ///
    /// let s = snapshot(v)?;
    /// assert_eq!(s.shared(), 0);
    ///
    /// // An iterators takes a shared reference to the collection being iterated over.
    /// let it = v.iter();
    ///
    /// let s = snapshot(v)?;
    /// assert_eq!(s.shared(), 1);
    /// drop(it);
    ///
    /// let s = snapshot(v)?;
    /// assert_eq!(s.shared(), 0);
    /// ```
    #[rune::function]
    fn shared(&self) -> usize {
        self.inner.shared()
    }

    /// Test if the snapshot indicates that the value is exclusively held.
    ///
    /// # Examples
    ///
    /// ```rune
    /// use std::mem::snapshot;
    ///
    /// let v = [1, 2, 3];
    ///
    /// let s = snapshot(v)?;
    /// assert_eq!(s.shared(), 0);
    /// assert!(!s.is_exclusive());
    /// assert!(s.is_readable());
    /// assert!(s.is_writable());
    ///
    /// // Assign to a separate variable since the compiler will notice that `v` is moved.
    /// let u = v;
    ///
    /// // Move the value into a closure, causing the original reference to become exclusively held.
    /// let closure = move || {
    ///    v
    /// };
    ///
    /// let s = snapshot(u)?;
    /// assert!(s.is_exclusive());
    /// assert!(!s.is_readable());
    /// assert!(!s.is_writable());
    /// ```
    #[rune::function]
    fn is_exclusive(&self) -> bool {
        self.inner.is_exclusive()
    }

    /// Test if the snapshot indicates that the value is readable.
    ///
    /// # Examples
    ///
    /// ```rune
    /// use std::mem::snapshot;
    ///
    /// let v = [1, 2, 3];
    ///
    /// let s = snapshot(v)?;
    /// assert_eq!(s.shared(), 0);
    /// assert!(!s.is_exclusive());
    /// assert!(s.is_readable());
    /// assert!(s.is_writable());
    ///
    /// // Assign to a separate variable since the compiler will notice that `v` is moved.
    /// let u = v;
    ///
    /// // Move the value into a closure, causing the original reference to become exclusively held.
    /// let closure = move || {
    ///    v
    /// };
    ///
    /// let s = snapshot(u)?;
    /// assert!(s.is_exclusive());
    /// assert!(!s.is_readable());
    /// assert!(!s.is_writable());
    /// ```
    #[rune::function]
    fn is_readable(&self) -> bool {
        self.inner.is_readable()
    }

    /// Test if the snapshot indicates that the value is writable.
    ///
    /// # Examples
    ///
    /// ```rune
    /// use std::mem::snapshot;
    ///
    /// let v = [1, 2, 3];
    ///
    /// let s = snapshot(v)?;
    /// assert_eq!(s.shared(), 0);
    /// assert!(!s.is_exclusive());
    /// assert!(s.is_readable());
    /// assert!(s.is_writable());
    ///
    /// // Assign to a separate variable since the compiler will notice that `v` is moved.
    /// let u = v;
    ///
    /// // Move the value into a closure, causing the original reference to become exclusively held.
    /// let closure = move || {
    ///    v
    /// };
    ///
    /// let s = snapshot(u)?;
    /// assert!(s.is_exclusive());
    /// assert!(!s.is_readable());
    /// assert!(!s.is_writable());
    /// ```
    #[rune::function]
    fn is_writable(&self) -> bool {
        self.inner.is_writable()
    }

    #[rune::function(protocol = DISPLAY_FMT)]
    fn display(&self, f: &mut Formatter) -> VmResult<()> {
        vm_write!(f, "{}", self.inner)
    }

    #[rune::function(protocol = DEBUG_FMT)]
    fn debug(&self, f: &mut Formatter) -> VmResult<()> {
        vm_write!(f, "{:?}", self.inner)
    }
}

/// Explicitly drop the given value, freeing up any memory associated with it.
///
/// Normally values are dropped as they go out of scope, but with this method it
/// can be explicitly controlled instead.
///
/// # Examples
///
/// Basic usage:
///
/// ```rune
/// let v = [1, 2, 3];
/// drop(v);
/// ```
#[rune::function]
fn drop(value: Value) -> VmResult<()> {
    vm_try!(value.drop());
    VmResult::Ok(())
}

/// Get the usage snapshot of a value.
///
/// A snapshot can be used to diagnose how many users a given value has.
///
/// # Examples
///
/// ```rune
/// use std::mem::snapshot;
///
/// let v1 = [1, 2, 3];
/// let a = snapshot(v1)?;
/// let v2 = [v1];
/// let b = snapshot(v1)?;
///
/// assert_eq!(a.shared(), 0);
/// assert_eq!(b.shared(), 0);
///
/// dbg!(a);
/// dbg!(b);
/// ```
#[rune::function]
fn snapshot(value: Value) -> Option<Snapshot> {
    value
        .snapshot()
        .map(|snapshot| Snapshot { inner: snapshot })
}