evil_janet/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(clippy::missing_safety_doc)]
5#![no_std]
6
7extern crate alloc;
8
9use core::{
10    cmp::Ordering,
11    fmt,
12    hash::{Hash, Hasher},
13};
14
15include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
16
17impl fmt::Debug for Janet {
18    #[inline]
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        f.pad(core::any::type_name::<Self>())
21    }
22}
23
24impl PartialEq<Janet> for Janet {
25    #[inline]
26    fn eq(&self, other: &Janet) -> bool {
27        unsafe { janet_equals(*self, *other) != 0 }
28    }
29}
30
31impl Eq for Janet {}
32
33impl Hash for Janet {
34    #[inline]
35    fn hash<H: Hasher>(&self, state: &mut H) {
36        state.write_i32(unsafe { janet_hash(*self) })
37    }
38}
39
40impl PartialOrd<Janet> for Janet {
41    #[inline]
42    fn partial_cmp(&self, other: &Janet) -> Option<Ordering> {
43        Some(self.cmp(other))
44    }
45}
46
47impl Ord for Janet {
48    #[inline]
49    fn cmp(&self, other: &Self) -> Ordering {
50        let res = unsafe { janet_compare(*self, *other) };
51
52        match res {
53            -1 => Ordering::Less,
54            0 => Ordering::Equal,
55            1 => Ordering::Greater,
56            // SAFETY: Janet ensures that the only values that `janet_compare` will return is `0`,
57            // `1` and `-1`.
58            _ => unsafe { core::hint::unreachable_unchecked() },
59        }
60    }
61}