pyruts 0.1.0

面向 Rust 工程实践的 proc_macro 工具集: Builder derive, c_wrap! FFI 包装, ensure!/bail!/throw! 错误宏, dict!/btree_dict! 容器字面量
Documentation
use pyruts::c_wrap;
use std::os::raw::c_char;

// ── 模拟 C 函数: 接收 *const *const c_char (execv 风格字符串数组, null 终止) ──
mod native {
    use std::os::raw::c_char;

    // 统计 argv 数组中非 null 元素个数 (遇 null 终止符停止)
    #[no_mangle]
    pub extern "C" fn my_count_args(argv: *const *const c_char) -> i32 {
        if argv.is_null() {
            return -1;
        }
        let mut count = 0;
        unsafe {
            let mut p = argv;
            while !(*p).is_null() {
                count += 1;
                p = p.add(1);
            }
        }
        count
    }

    // 返回 argv[0] (第一个字符串), 若数组为空或 null 返回 null
    #[no_mangle]
    pub extern "C" fn my_first_arg(argv: *const *const c_char) -> *const c_char {
        unsafe {
            if argv.is_null() || (*argv).is_null() {
                return std::ptr::null();
            }
            *argv
        }
    }

    // 统计所有字符串的总字节数 (不含终止符)
    #[no_mangle]
    pub extern "C" fn my_total_bytes(argv: *const *const c_char) -> usize {
        if argv.is_null() {
            return 0;
        }
        let mut total = 0;
        unsafe {
            let mut p = argv;
            while !(*p).is_null() {
                let mut q = *p;
                while *q != 0 {
                    total += 1;
                    q = q.add(1);
                }
                p = p.add(1);
            }
        }
        total
    }
}

c_wrap! {
    extern "C" {
        fn my_count_args(argv: *const *const c_char) -> i32;
        fn my_first_arg(argv: *const *const c_char) -> *const c_char;
        fn my_total_bytes(argv: *const *const c_char) -> usize;
    }
}

#[test]
fn c_wrap_str_array_count_three() {
    assert_eq!(safe_my_count_args(&["a", "b", "c"]), 3);
}

#[test]
fn c_wrap_str_array_empty() {
    // 空数组: Vec 只含 null 终止符, C 函数立即遇到 null, count = 0
    assert_eq!(safe_my_count_args(&[]), 0);
}

#[test]
fn c_wrap_str_array_single() {
    assert_eq!(safe_my_count_args(&["only"]), 1);
}

#[test]
fn c_wrap_str_array_first_arg() {
    assert_eq!(safe_my_first_arg(&["hello", "world"]), "hello");
}

#[test]
fn c_wrap_str_array_total_bytes() {
    let total = safe_my_total_bytes(&["foo", "bar", "baz"]);
    assert_eq!(total, 9);
}

#[test]
fn c_wrap_str_array_total_bytes_empty() {
    assert_eq!(safe_my_total_bytes(&[]), 0);
}

#[test]
fn c_wrap_str_array_accepts_vec_slice() {
    // &[&str] 也接受 Vec<&str>.as_slice() 的结果
    let v: Vec<&str> = vec!["x", "y", "z"];
    assert_eq!(safe_my_count_args(v.as_slice()), 3);
}

#[test]
fn c_wrap_str_array_accepts_array_ref() {
    // &[&str; N] 会 coerce 到 &[&str]
    let arr: [&str; 4] = ["1", "2", "3", "4"];
    assert_eq!(safe_my_count_args(&arr), 4);
}

#[test]
fn c_wrap_str_array_longer_strings() {
    assert_eq!(
        safe_my_total_bytes(&["hello", "world", "rust", "proc_macro"]),
        "hello".len() + "world".len() + "rust".len() + "proc_macro".len()
    );
}