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
215
216
217
218
219
220
221
// Copyright (c) SimpleStaking and Tezedge Contributors
// SPDX-License-Identifier: MIT

use crate::value::OCaml;
use crate::{
    memory::{
        alloc_bytes, alloc_cons, alloc_double, alloc_int32, alloc_int64, alloc_some, alloc_string,
        alloc_tuple, alloc_tuple_3, alloc_tuple_4, OCamlAllocResult, OCamlAllocToken,
    },
    OCamlRef,
};
use crate::{
    mlvalues::{
        OCamlBytes, OCamlInt, OCamlInt32, OCamlInt64, OCamlList, RawOCaml, FALSE, NONE, TRUE,
    },
    OCamlFloat,
};
use crate::{ocaml_alloc, ocaml_frame, to_ocaml};
use core::str;

/// Implements conversion from Rust values into OCaml values.
pub unsafe trait ToOCaml<T> {
    /// Convert to OCaml value.
    ///
    /// Should not be called directly, use [`to_ocaml!`] macro instead.
    /// If called directly, the call should be wrapped by [`ocaml_alloc!`].
    fn to_ocaml(&self, gc: OCamlAllocToken) -> OCamlAllocResult<T>;
}

unsafe impl<'a, T> ToOCaml<T> for OCamlRef<'a, T> {
    fn to_ocaml(&self, _token: OCamlAllocToken) -> OCamlAllocResult<T> {
        OCamlAllocResult::of(self.get_raw())
    }
}

unsafe impl ToOCaml<OCamlInt> for i64 {
    fn to_ocaml(&self, _token: OCamlAllocToken) -> OCamlAllocResult<OCamlInt> {
        OCamlAllocResult::of(((self << 1) | 1) as RawOCaml)
    }
}

unsafe impl ToOCaml<OCamlInt> for i32 {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlInt> {
        (*self as i64).to_ocaml(token)
    }
}

unsafe impl ToOCaml<OCamlInt32> for i32 {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlInt32> {
        alloc_int32(token, *self)
    }
}

unsafe impl ToOCaml<OCamlInt64> for i64 {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlInt64> {
        alloc_int64(token, *self)
    }
}

unsafe impl ToOCaml<OCamlFloat> for f64 {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlFloat> {
        alloc_double(token, *self)
    }
}

unsafe impl ToOCaml<bool> for bool {
    fn to_ocaml(&self, _token: OCamlAllocToken) -> OCamlAllocResult<bool> {
        OCamlAllocResult::of(if *self { TRUE } else { FALSE })
    }
}

unsafe impl ToOCaml<String> for &str {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<String> {
        alloc_string(token, self)
    }
}

unsafe impl ToOCaml<OCamlBytes> for &str {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlBytes> {
        alloc_bytes(token, self.as_bytes())
    }
}

unsafe impl ToOCaml<OCamlBytes> for &[u8] {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlBytes> {
        alloc_bytes(token, self)
    }
}

unsafe impl ToOCaml<String> for &[u8] {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<String> {
        alloc_string(token, unsafe { str::from_utf8_unchecked(self) })
    }
}

unsafe impl ToOCaml<String> for String {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<String> {
        self.as_str().to_ocaml(token)
    }
}

unsafe impl ToOCaml<OCamlBytes> for String {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlBytes> {
        self.as_str().to_ocaml(token)
    }
}

unsafe impl ToOCaml<String> for Vec<u8> {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<String> {
        self.as_slice().to_ocaml(token)
    }
}

unsafe impl ToOCaml<OCamlBytes> for Vec<u8> {
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlBytes> {
        self.as_slice().to_ocaml(token)
    }
}

unsafe impl<A, OCamlA> ToOCaml<OCamlA> for Box<A>
where
    A: ToOCaml<OCamlA>,
{
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlA> {
        self.as_ref().to_ocaml(token)
    }
}

unsafe impl<A, OCamlA> ToOCaml<Option<OCamlA>> for Option<A>
where
    A: ToOCaml<OCamlA>,
{
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<Option<OCamlA>> {
        if let Some(value) = self {
            ocaml_frame!(gc(ocaml_value), {
                let ocaml_value = &ocaml_value.keep(to_ocaml!(gc, value));
                alloc_some(token, ocaml_value)
            })
        } else {
            OCamlAllocResult::of(NONE)
        }
    }
}

unsafe impl<A, B, OCamlA, OCamlB> ToOCaml<(OCamlA, OCamlB)> for (A, B)
where
    A: ToOCaml<OCamlA>,
    B: ToOCaml<OCamlB>,
{
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<(OCamlA, OCamlB)> {
        ocaml_frame!(gc(fst, snd), {
            let fst = &fst.keep(to_ocaml!(gc, self.0));
            let snd = &snd.keep(to_ocaml!(gc, self.1));
            alloc_tuple(token, fst, snd)
        })
    }
}

unsafe impl<A, B, C, OCamlA, OCamlB, OCamlC> ToOCaml<(OCamlA, OCamlB, OCamlC)> for (A, B, C)
where
    A: ToOCaml<OCamlA>,
    B: ToOCaml<OCamlB>,
    C: ToOCaml<OCamlC>,
{
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<(OCamlA, OCamlB, OCamlC)> {
        ocaml_frame!(gc(fst, snd, elt3), {
            let fst = &fst.keep(to_ocaml!(gc, self.0));
            let snd = &snd.keep(to_ocaml!(gc, self.1));
            let elt3 = &elt3.keep(to_ocaml!(gc, self.2));
            alloc_tuple_3(token, fst, snd, elt3)
        })
    }
}

unsafe impl<A, B, C, D, OCamlA, OCamlB, OCamlC, OCamlD> ToOCaml<(OCamlA, OCamlB, OCamlC, OCamlD)>
    for (A, B, C, D)
where
    A: ToOCaml<OCamlA>,
    B: ToOCaml<OCamlB>,
    C: ToOCaml<OCamlC>,
    D: ToOCaml<OCamlD>,
{
    fn to_ocaml(
        &self,
        token: OCamlAllocToken,
    ) -> OCamlAllocResult<(OCamlA, OCamlB, OCamlC, OCamlD)> {
        ocaml_frame!(gc(fst, snd, elt3, elt4), {
            let fst = &fst.keep(to_ocaml!(gc, self.0));
            let snd = &snd.keep(to_ocaml!(gc, self.1));
            let elt3 = &elt3.keep(to_ocaml!(gc, self.2));
            let elt4 = &elt4.keep(to_ocaml!(gc, self.3));
            alloc_tuple_4(token, fst, snd, elt3, elt4)
        })
    }
}

unsafe impl<A, OCamlA> ToOCaml<OCamlList<OCamlA>> for Vec<A>
where
    A: ToOCaml<OCamlA>,
{
    fn to_ocaml(&self, token: OCamlAllocToken) -> OCamlAllocResult<OCamlList<OCamlA>> {
        (&self).to_ocaml(token)
    }
}

unsafe impl<A, OCamlA> ToOCaml<OCamlList<OCamlA>> for &Vec<A>
where
    A: ToOCaml<OCamlA>,
{
    fn to_ocaml(&self, _token: OCamlAllocToken) -> OCamlAllocResult<OCamlList<OCamlA>> {
        ocaml_frame!(gc(result_ref, ov_ref), {
            let result_ref = &mut result_ref.keep(OCaml::nil());
            for elt in self.iter().rev() {
                let ov = &ov_ref.keep(to_ocaml!(gc, elt));
                let cons = ocaml_alloc!(alloc_cons(gc, ov, result_ref));
                result_ref.set(cons);
            }
            OCamlAllocResult::of_ocaml(gc.get(result_ref))
        })
    }
}