Skip to main content

dodo_bindgen_utils/
lib.rs

1#![allow(unexpected_cfgs)]
2
3use dodo_bindgen_macros::dodo_bindgen;
4
5mod dodo_bindgen {
6    #[allow(dead_code)]
7    pub(crate) trait DenoBindgen {}
8}
9
10struct Metadata;
11impl dodo_bindgen::DenoBindgen for Metadata {}
12#[dodo_bindgen]
13impl Metadata {
14    fn rust_version() -> *const u8 {
15        concat!(env!("CARGO_PKG_RUST_VERSION"), "\0").as_ptr()
16    }
17    fn lib_name() -> *const u8 {
18        concat!(env!("CARGO_CRATE_NAME"), "\0").as_ptr()
19    }
20    fn lib_version() -> *const u8 {
21        concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr()
22    }
23}
24
25#[allow(dead_code)]
26struct RustString;
27impl dodo_bindgen::DenoBindgen for RustString {}
28#[dodo_bindgen]
29#[cfg(dodo_bindgen_rust_string)]
30impl RustString {
31    pub fn new() -> String {
32        String::new()
33    }
34    pub fn from(ptr: *const u8, len: usize) -> String {
35        String::from_utf8(unsafe { std::slice::from_raw_parts(ptr, len).to_vec() })
36            .expect("string must be utf-8")
37    }
38    pub fn into_ptr(string: &String) -> *const u8 {
39        string.as_ptr()
40    }
41    pub fn into_len(string: &String) -> usize {
42        string.len()
43    }
44    pub fn push(string: &mut String, ptr: *const u8, len: usize) {
45        let str = std::str::from_utf8(unsafe { std::slice::from_raw_parts(ptr, len) })
46            .expect("string must be utf-8");
47        string.push_str(str);
48    }
49    pub fn drop(string: String) {
50        std::mem::drop(string);
51    }
52}