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
//! Convert between Rust and Ruby objects.

/// Infallible conversion between two types.
///
/// Implementors may not allocate on the interpreter heap.
///
/// See [`core::convert::From`].
/// See [`ConvertMut`].
pub trait Convert<T, U> {
    /// Performs the infallible conversion.
    fn convert(&self, from: T) -> U;
}

/// Fallible conversions between two types.
///
/// Implementors may not allocate on the interpreter heap.
///
/// See [`core::convert::TryFrom`].
/// See [`TryConvertMut`].
pub trait TryConvert<T, U> {
    /// Error type for failed conversions.
    type Error;

    /// Performs the fallible conversion.
    ///
    /// # Errors
    ///
    /// If boxing or unboxing a value into the specified type fails, an error is
    /// returned.
    fn try_convert(&self, value: T) -> Result<U, Self::Error>;
}

/// Mutable infallible conversion between two types.
///
/// Implementors may allocate on the interpreter heap.
///
/// See [`core::convert::From`].
/// See [`Convert`].
pub trait ConvertMut<T, U> {
    /// Performs the infallible conversion.
    fn convert_mut(&mut self, from: T) -> U;
}

/// Mutable fallible conversions between two types.
///
/// Implementors may allocate on the interpreter heap.
///
/// See [`core::convert::TryFrom`].
/// See [`TryConvert`].
pub trait TryConvertMut<T, U> {
    /// Error type for failed conversions.
    type Error;

    /// Performs the fallible conversion.
    ///
    /// # Errors
    ///
    /// If boxing or unboxing a value into the specified type fails, an error is
    /// returned.
    fn try_convert_mut(&mut self, value: T) -> Result<U, Self::Error>;
}