dodo-bindgen-utils 1.0.2

Contains utilities for interacting with rust data structures through FFI. Conditionally-compiled and embedded into the final shared library by the CLI tool
Documentation
#![allow(unexpected_cfgs)]

use dodo_bindgen_macros::dodo_bindgen;

mod dodo_bindgen {
    #[allow(dead_code)]
    pub(crate) trait DenoBindgen {}
}

struct Metadata;
impl dodo_bindgen::DenoBindgen for Metadata {}
#[dodo_bindgen]
impl Metadata {
    fn rust_version() -> *const u8 {
        concat!(env!("CARGO_PKG_RUST_VERSION"), "\0").as_ptr()
    }
    fn lib_name() -> *const u8 {
        concat!(env!("CARGO_CRATE_NAME"), "\0").as_ptr()
    }
    fn lib_version() -> *const u8 {
        concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr()
    }
}

#[allow(dead_code)]
struct RustString;
impl dodo_bindgen::DenoBindgen for RustString {}
#[dodo_bindgen]
#[cfg(dodo_bindgen_rust_string)]
impl RustString {
    pub fn new() -> String {
        String::new()
    }
    pub fn from(ptr: *const u8, len: usize) -> String {
        String::from_utf8(unsafe { std::slice::from_raw_parts(ptr, len).to_vec() })
            .expect("string must be utf-8")
    }
    pub fn into_ptr(string: &String) -> *const u8 {
        string.as_ptr()
    }
    pub fn into_len(string: &String) -> usize {
        string.len()
    }
    pub fn push(string: &mut String, ptr: *const u8, len: usize) {
        let str = std::str::from_utf8(unsafe { std::slice::from_raw_parts(ptr, len) })
            .expect("string must be utf-8");
        string.push_str(str);
    }
    pub fn drop(string: String) {
        std::mem::drop(string);
    }
}