deno_bindgen2_utils/
lib.rs

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