Rust插件库doe的使用:探索高性能Rust扩展功能库doe的特性与应用
Rust插件库doe的使用:探索高性能Rust扩展功能库doe的特性与应用
Doe是一个多功能Rust工具库,通过提供大量有用的宏和实用函数显著增强了开发工作流程。它简化了常见任务,并为剪贴板管理、状态管理、键盘输入和鼠标交互提供了便捷功能。此外,doe还包含强大的加密功能,使开发人员可以轻松地将安全加密、解密和哈希功能集成到应用程序中,确保数据完整性和机密性。
核心特性
- 剪贴板管理:简化剪贴板操作
- 加密功能:AES、RSA、SHA、MD5和Blake3实现
- 文件系统工具:高级文件操作和管理
- 随机数生成:基于LCG的随机数生成器
- 系统工具:进程管理和系统命令
可选特性
要使用特定功能,请在Cargo.toml中添加它们:
[dependencies]
doe = { version = "1.0", features = ["ctf", "clip", "mouse"] }
使用示例
加密示例
let data = "this is data".as_bytes();
let enc_data = doe::crypto::aes::aes_ecb_encrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),data).unwrap();
let dec_data = doe::crypto::aes::aes_ecb_decrypt("12345678123456781234567812345678".as_bytes(),"1234567812345678".as_bytes(),&enc_data).unwrap();
println!("data:{:?}",&data);
println!("enc_data:{:?}",&enc_data);
println!("dec_data:{:?}",&dec_data);
println!("blake3:{:?}",doe::crypto::blake3_xof("dnrops".as_bytes(), 10));
键盘鼠标自动化示例
// cargo add doe -F kcm
use doe::keyboard::key_click;
use doe::keyboard::key_press;
use doe::keyboard::key_release;
use doe::keyboard::KeyCode;
use doe::mouse::mouse_drag;
use doe::mouse::move_to_paste;
use doe::mouse::move_to_press_left;
use doe::*;
use keyboard::exit_if_click_esc;
let list = vec!["iphone","ipad","macbook"];
// crete new baogao
move_to_press_left(857, 588).sleep(1.0); //#000000
// create fengmian
move_to_paste(1540, 853, "Apple").sleep(1.0); //#000000
move_to_paste(1360, 882, "ID").sleep(1.0); //#000000
// add shuoming
move_to_paste(772, 464, "Discription").sleep(1.0); //#ffffff
mouse_drag((894, 719).into(), (821, 716).into()).sleep(1.0); //#f0f0f0
key_click("2024-01-23").sleep(1.0);
move_to_press_left(740, 305).sleep(1.0); //#f0f0f0
for name in list {
// add baobao
move_to_press_left(476, 253).sleep(1.0); //#ffffff
// name
move_to_press_left(796, 331).sleep(1.0); //#ffffff
key_click("end");
key_click("shift+home");
set_clipboard(name).unwrap().sleep(0.5);
key_click("ctrl+v");
// add fujian
move_to_press_left(870, 587).sleep(1.0); //#000000
mouse_drag((893, 818).into(), (814, 816).into()).sleep(1.0); //#f0f0f0
key_click("2024-01-23").sleep(1.0);
move_to_press_left(723, 206).sleep(1.0); //#000000
}
// set taoshu
move_to_paste(1341, 910, "New_name").sleep(1.0); //#000000
Word文档替换示例
//finds all 'name' and replace with 'andrew'
use doe::*;
docx::docx_replace("./name.docx","name","andrew").unwrap();
docx::docx_get_content("./name.docx").unwrap().dprintln();
docx::docx_remove_read_only("./name.docx").unwrap();
完整示例代码
// 完整示例:使用doe库进行加密和解密操作
use doe::crypto::aes;
fn main() {
// 原始数据
let plaintext = "这是一个秘密消息".as_bytes();
// 128位密钥 (16字节)
let key = "my_super_secret_key".as_bytes();
// 初始向量 (IV) - 16字节
let iv = "initial_vector_123".as_bytes();
// 加密数据
let ciphertext = aes::aes_ecb_encrypt(key, iv, plaintext)
.expect("加密失败");
println!("加密后的数据: {:?}", ciphertext);
// 解密数据
let decrypted = aes::aes_ecb_decrypt(key, iv, &ciphertext)
.expect("解密失败");
println!("解密后的数据: {:?}", String::from_utf8_lossy(&decrypted));
}
这个示例展示了如何使用doe库的AES ECB模式加密和解密功能。首先定义原始数据、密钥和初始向量,然后进行加密和解密操作,最后打印结果。
1 回复
Rust插件库doe的使用:探索高性能Rust扩展功能库doe的特性与应用
doe库简介
doe是一个高性能的Rust扩展功能库,专注于为Rust开发者提供高效的插件式扩展能力。它通过轻量级的接口设计和优化的内存管理,使得开发者可以轻松地为应用程序添加模块化功能。
主要特性
- 高性能插件架构:低开销的插件加载和执行机制
- 热重载支持:无需重启应用即可更新插件
- 跨平台兼容:支持Windows、Linux和macOS
- 内存安全:严格遵循Rust所有权模型
- 简单易用的API:直观的接口设计
安装方法
在Cargo.toml中添加依赖:
[dependencies]
doe = "0.4.2"
完整示例demo
下面是一个完整的doe库使用示例,包含插件创建、加载、使用和热重载功能:
1. 创建Cargo项目
cargo new doe_example
cd doe_example
2. 添加依赖
编辑Cargo.toml文件:
[package]
name = "doe_example"
version = "0.1.0"
edition = "2021"
[dependencies]
doe = "0.4.2"
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3"
notify = "5.0"
3. 创建插件项目
cargo new --lib my_plugin
cd my_plugin
编辑my_plugin/Cargo.toml:
[package]
name = "my_plugin"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
doe = "0.4.2"
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3"
4. 编写插件代码
my_plugin/src/lib.rs:
use doe::{Plugin, PluginResult};
use serde::{Serialize, Deserialize};
// 插件必须导出的创建函数
#[no_mangle]
pub extern "C" fn create_plugin() -> *mut dyn Plugin {
Box::into_raw(Box::new(MyPlugin))
}
// 定义插件结构体
struct MyPlugin;
// 实现Plugin trait
impl Plugin for MyPlugin {
fn name(&self) -> &str {
"MyPlugin"
}
// 简单处理字符串
fn execute(&self, input: &str) -> PluginResult<String> {
Ok(format!("Processed: {}", input.to_uppercase()))
}
// 处理二进制数据
fn execute_bytes(&self, input: &[u8]) -> PluginResult<Vec<u8>> {
#[derive(Serialize, Deserialize)]
struct PluginData {
value: String,
count: usize,
}
// 反序列化输入数据
let data: PluginData = bincode::deserialize(input)?;
// 处理数据
let processed = PluginData {
value: data.value.to_uppercase(),
count: data.count * 2,
};
// 返回序列化结果
Ok(bincode::serialize(&processed)?)
}
}
5. 编写主程序代码
src/main.rs:
use doe::{PluginManager, Plugin};
use std::path::Path;
use std::time::Duration;
use notify::{RecommendedWatcher, Watcher, RecursiveMode};
use serde::{Serialize, Deserialize};
// 定义共享数据结构
#[derive(Serialize, Deserialize, Debug)]
struct SharedData {
counter: i32,
message: String,
}
fn main() {
println!("Starting doe example application...");
// 初始化插件管理器
let mut manager = PluginManager::new();
// 加载插件
let plugin_path = Path::new("./my_plugin/target/debug/libmy_plugin.so");
if let Err(e) = manager.load(plugin_path) {
eprintln!("Failed to load plugin: {}", e);
return;
}
println!("Plugin loaded successfully");
// 示例1: 基本插件使用
if let Some(plugin) = manager.get_plugin("MyPlugin") {
match plugin.execute("hello doe") {
Ok(result) => println!("Basic plugin result: {}", result),
Err(e) => eprintln!("Plugin error: {}", e),
}
}
// 示例2: 插件间二进制数据通信
let data = SharedData {
counter: 42,
message: "Hello from host".to_string(),
};
let serialized = bincode::serialize(&data).unwrap();
if let Some(plugin) = manager.get_plugin("MyPlugin") {
match plugin.execute_bytes(&serialized) {
Ok(result) => {
let response: SharedData = bincode::deserialize(&result).unwrap();
println!("Binary communication result: {:?}", response);
},
Err(e) => eprintln!("Binary communication error: {}", e),
}
}
// 示例3: 热重载演示
println!("Setting up hot reload watcher...");
let mut watcher: RecommendedWatcher = Watcher::new_immediate(|res| {
if let Ok(event) = res {
if event.kind.is_modify() {
println!("Plugin file modified, attempting reload...");
if let Err(e) = manager.reload_all() {
eprintln!("Reload failed: {}", e);
} else {
println!("Plugins reloaded successfully");
}
}
}
}).unwrap();
watcher.watch("./my_plugin/target/debug", RecursiveMode::Recursive).unwrap();
// 主循环
println!("Entering main loop. Press Ctrl+C to exit.");
let mut counter = 0;
loop {
if let Some(plugin) = manager.get_plugin("MyPlugin") {
match plugin.execute(&format!("loop {}", counter)) {
Ok(result) => println!("Loop result: {}", result),
Err(e) => eprintln!("Loop error: {}", e),
}
}
counter += 1;
std::thread::sleep(Duration::from_secs(2));
}
}
6. 构建和运行
- 首先构建插件:
cd my_plugin
cargo build
cd ..
- 然后运行主程序:
cargo run
7. 热重载测试
在程序运行时,可以修改my_plugin/src/lib.rs中的代码,例如修改execute方法:
fn execute(&self, input: &str) -> PluginResult<String> {
Ok(format!("MODIFIED: {}", input.to_uppercase()))
}
然后重新构建插件:
cd my_plugin
cargo build
cd ..
观察主程序输出,你会看到插件被自动重新加载,并且新的处理逻辑立即生效。
性能优化建议
- 尽量减少插件与主机之间的数据拷贝
- 对大块数据使用引用或共享内存
- 考虑使用零拷贝序列化格式如bincode
- 避免频繁的插件加载/卸载操作
实际应用场景
- 游戏引擎的脚本扩展
- 数据分析管道的可插拔处理模块
- 网络服务的协议处理插件
- 图形应用程序的滤镜/特效系统
doe库通过其简洁的API和高性能设计,使得Rust应用程序能够轻松实现模块化和可扩展架构,同时保持Rust的内存安全和性能优势。