ocaml_interop/
mlvalues.rs

1// Copyright (c) Viable Systems and TezEdge Contributors
2// SPDX-License-Identifier: MIT
3
4#[cfg(doc)]
5use crate::*;
6
7use core::marker::PhantomData;
8pub use ocaml_sys::{
9    extract_exception, field as field_val, is_block, is_exception_result, is_long, string_val,
10    tag_val, wosize_val, Intnat, Uintnat as UIntnat, Value as RawOCaml, EMPTY_LIST, FALSE,
11    MAX_FIXNUM, MIN_FIXNUM, NONE, TRUE, UNIT,
12};
13
14pub mod bigarray;
15pub mod tag;
16
17pub(crate) unsafe fn int32_val(v: RawOCaml) -> i32 {
18    let val = unsafe { field_val(v, 1) };
19    unsafe { *(val as *const i32) }
20}
21
22pub(crate) unsafe fn int64_val(v: RawOCaml) -> i64 {
23    let val = unsafe { field_val(v, 1) };
24    unsafe { *(val as *const i64) }
25}
26
27/// [`OCaml`]`<OCamlList<T>>` is a reference to an OCaml `list` containing
28/// values of type `T`.
29pub struct OCamlList<A> {
30    _marker: PhantomData<A>,
31}
32
33/// [`OCaml`]`<OCamlUniformArray<T>>` is a reference to an OCaml array which is
34/// guaranteed to not contain unboxed floats. If OCaml was configured with
35/// `--disable-flat-float-array` this corresponds to regular `array`s, but if
36/// not, `Uniform_array.t` in the `base` library can be used instead.
37/// See [Lexifi's blog post on the topic](https://www.lexifi.com/blog/ocaml/about-unboxed-float-arrays/)
38/// for more details.
39pub struct OCamlUniformArray<A> {
40    _marker: PhantomData<A>,
41}
42
43/// [`OCaml`]`<OCamlFloatArray<T>>` is a reference to an OCaml `floatarray`
44/// which is an array containing `float`s in an unboxed form.
45pub struct OCamlFloatArray {}
46
47/// `OCaml<DynBox<T>>` is for passing a value of type `T` to OCaml
48///
49/// To box a Rust value, use [`OCaml::box_value`][crate::OCaml::box_value].
50///
51/// **Experimental**
52pub struct DynBox<A> {
53    _marker: PhantomData<A>,
54}
55
56/// [`OCaml`]`<OCamlBytes>` is a reference to an OCaml `bytes` value.
57///
58/// # Note
59///
60/// Unlike with [`OCaml`]`<String>`, there is no validation being performed when converting this
61/// value into `String`.
62pub struct OCamlBytes {}
63
64/// [`OCaml`]`<OCamlInt>` is an OCaml integer (tagged and unboxed) value.
65pub type OCamlInt = Intnat;
66
67/// [`OCaml`]`<OCamlInt32>` is a reference to an OCaml `Int32.t` (boxed `int32`) value.
68pub struct OCamlInt32 {}
69
70/// [`OCaml`]`<OCamlInt64>` is a reference to an OCaml `Int64.t` (boxed `int64`) value.
71pub struct OCamlInt64 {}
72
73/// [`OCaml`]`<OCamlFloat>` is a reference to an OCaml `float` (boxed `float`) value.
74pub struct OCamlFloat {}
75
76/// [`OCaml`]`<OCamlException>` is a reference to an OCaml `exn` value.
77pub struct OCamlException {}