lightning_sys/
types.rs

1use crate::bindings;
2use std::ptr::null_mut;
3use std::ffi::c_void;
4
5#[derive(Copy,Clone,PartialEq,Eq,Debug)]
6pub enum Reg {
7    R(bindings::jit_gpr_t),
8    V(bindings::jit_gpr_t),
9    F(bindings::jit_gpr_t),
10    FP,
11}
12
13pub struct JitNode<'a> {
14    pub(crate) node:  *mut bindings::jit_node_t,
15    pub(crate) phantom: std::marker::PhantomData<&'a ()>,
16    //pub(crate) state: &'a JitState<'a>,
17}
18
19pub type JitWord = bindings::jit_word_t;
20pub type JitUword = bindings::jit_uword_t;
21pub type JitPointer = bindings::jit_pointer_t;
22
23pub const NULL: JitPointer = null_mut::<c_void>();
24
25pub(crate) trait ToFFI {
26    type Type;
27    fn to_ffi(&self) -> Self::Type;
28}
29
30impl ToFFI for Reg {
31    type Type = bindings::jit_gpr_t;
32
33    //TODO: safe conversion
34    fn to_ffi(&self) -> Self::Type {
35        match self {
36            Reg::R(i) => if *i < unsafe { bindings::lgsys_JIT_R_NUM() } {
37                unsafe { bindings::lgsys_jit_r(*i) }
38            } else {
39                panic!("register 'R{}' is not supported", *i);
40            },
41
42            Reg::V(i) => if *i < unsafe { bindings::lgsys_JIT_V_NUM() } {
43                unsafe { bindings::lgsys_jit_v(*i) }
44            } else {
45                panic!("register 'V{}' is not supported", i);
46            },
47
48            Reg::F(i) => if *i < unsafe { bindings::lgsys_JIT_F_NUM() } {
49                unsafe { bindings::lgsys_jit_f(*i) }
50            } else {
51                panic!("register 'F{}' is not supported", i);
52            },
53
54            Reg::FP => unsafe { bindings::lgsys_JIT_FP },
55        }
56    }
57}
58
59impl<'a> ToFFI for JitNode<'a> {
60    type Type = *mut bindings::jit_node_t;
61
62    fn to_ffi(&self) -> Self::Type {
63        self.node
64    }
65}
66
67pub(crate) trait FFISafe: Copy{}
68
69// this is dumb, blame rust
70impl FFISafe for JitPointer{}
71impl FFISafe for i32{}
72impl FFISafe for u32{}
73impl FFISafe for i64{}
74impl FFISafe for u64{}
75impl FFISafe for f32{}
76impl FFISafe for f64{}
77
78
79impl<T: FFISafe> ToFFI for T {
80    type Type = T;
81
82    fn to_ffi(&self) -> Self::Type {
83        *self
84    }
85}