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
//! The cloning trait for Rune.
use crate as rune;
use crate::runtime::{Protocol, Value, VmResult};
use crate::{ContextError, Module};
/// Cloning for Rune.
///
/// This module defines methods and types used when cloning values.
///
/// By default all values in rune are structurally shared, so in order to get a
/// unique instance of it you must clone it.
#[rune::module(::std::clone)]
pub fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
m.function_meta(clone)?;
let mut t = m.define_trait(["Clone"])?;
t.docs(docstring! {
/// The `Clone` trait is used to explicitly clone values.
///
/// # Examples
///
/// ```rune
/// let a = 42;
/// let b = a.clone();
///
/// assert_eq!(a, b);
/// ```
})?;
t.handler(|cx| {
_ = cx.find(&Protocol::CLONE)?;
Ok(())
})?;
t.function("clone")?
.argument_types::<(Value,)>()?
.return_type::<Value>()?
.docs(docstring! {
/// Clone the specified `value`.
///
/// # Examples
///
/// ```rune
/// let a = 42;
/// let b = a;
/// let c = a.clone();
///
/// a += 1;
///
/// assert_eq!(a, 43);
/// assert_eq!(b, 42);
/// assert_eq!(c, 42);
/// ```
})?;
Ok(m)
}
/// Clone the specified `value`.
///
/// # Examples
///
/// ```rune
/// let a = 42;
/// let b = a;
/// let c = clone(a);
///
/// a += 1;
///
/// assert_eq!(a, 43);
/// assert_eq!(b, 42);
/// assert_eq!(c, 42);
/// ```
#[rune::function]
fn clone(value: Value) -> VmResult<Value> {
value.clone_()
}