2个版本
| 0.1.6 | 2024年4月18日 | 
|---|---|
| 0.1.5 | 2024年4月16日 | 
#1107 in 进程宏
在2个crate中使用(通过e-utils)
22KB
102 行
📄 中文 | 📄 英文
⚡这是什么?
Rust宏
支持应用
| 应用 | Windows 10 | Unix | Macos | 
|---|---|---|---|
| Json | √ | √ | √ | 
| C | √ | √ | √ | 
| _ | × | × | × | 
✨功能
📖示例
[dependencies]
e-macros = "0.1"
#[derive(e_macros::C)]
struct B {
  d: i32,
  f: String,
}
fn test() -> Result<()> {
  // 假设我们有一个T类型的实例
  let value: B = B {
    d: 1,
    f: "test".to_string(),
  };
  let ptr = value.to_c_ptr();
  // 还原*c_void指针为<Box<T>>实例
  if let Some(restored_boxed_value) = unsafe { B::from_c_ptr(ptr) } {
    // 成功还原Box<T>实例
    println!("Restored value: {:?}", *restored_boxed_value);
  } else {
    // 还原过程中出现错误
    println!("Failed to restore value");
  }
  Ok(())
}
智能写入Json
示例
#[derive(serde::Deserialize, Debug, serde::Serialize, Default, e_macros::Json)]
struct B {
  d: i32,
  f: String,
}
fn test() {
  let mut b: B = B::default();
  b.f = "test".to_string();
  b.auto_write_json(Path::new("."), "test.json").unwrap();
  let b = B::auto_read_json(Path::new("test.json")).unwrap();
  println!("B {:?}", b);
}
智能读取Json
示例
#[derive(serde::Deserialize, Debug, serde::Serialize, Default, e_utils::Json)]
struct B {
  d: i32,
  f: String,
}
fn test() {
  let mut b: B = B::default();
  b.f = "test".to_string();
  b.auto_write_json(Path::new("."), "test.json").unwrap();
  let b = B::auto_read_json(Path::new("test.json")).unwrap();
  println!("B {:?}", b);
}
安全地将*const c_void指针还原为Box
示例
#[derive(e_utils::C)]
struct B {
  d: i32,
  f: String,
}
fn test() -> Result<()> {
  // 假设我们有一个T类型的实例
  let value: B = B {
    d: 1,
    f: "test".to_string(),
  };
  let ptr = value.to_c_ptr();
  // 还原*c_void指针为<Box<T>>实例
  if let Some(restored_boxed_value) = unsafe { B::from_c_ptr(ptr) } {
    // 成功还原Box<T>实例
    println!("Restored value: {:?}", *restored_boxed_value);
  } else {
    // 还原过程中出现错误
    println!("Failed to restore value");
  }
  Ok(())
}
💡!重要:
🚀运行速度快
🦊应用项目
🔭为什么需要e-utils?
🙋参考资料和材料
依赖
~260–700KB
~17K SLoC