# QRust **Repository Path**: dyf029/qrust ## Basic Information - **Project Name**: QRust - **Description**: QRust是Qt和Rust两种语言的混合编程中间件,是Qt调用Rust函数的支持技术。 - **Primary Language**: C++ - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-10-01 - **Last Updated**: 2024-10-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # QRust #### 介绍 QRust是Qt和Rust两种语言的混合编程中间件,是Qt调用Rust函数的支持技术。 QRust来源于工具软件OnTheSSH,OnTheSSH软件由Qt和Rust两种语言共同构建,Rust实现了SSH通讯底层协议,Qt搭建程序界面,Qt调用Rust的技术需求催生出了QRust。 #### 一个使用QRust的例子 Rust端: ``` fn invoke(fun_name: &str, mut args: Vec<&[u8]>) -> Result>> { match fun_name { …… "foo2" => { let a1 = de::from_pack(args.pop().unwrap())?; //反序列化获得参数1 let a2 = de::from_pack(args.pop().unwrap())?; //反序列化获得参数2 let a3 = de::from_pack(args.pop().unwrap())?; //反序列化获得参数3 let ret = api::foo2(a1, a2, a3); //调用函数foo2,得到返回值 let pack = ser::to_pack(&ret)?; //序列化返回值 Ok(Some(pack)) } …… } } ``` 在上面的代码中,通过匹配字符串”foo2″定位到函数api::foo2(),传入三个反序列化得到的参数,并将函数返回值序列化后返回。 Qt端: ``` Rust rust("foo2"); //声明要调用的Rust函数foo2 //参数 1 QList a1 = {100}; QByteArray ba1 = QRust_Ser::pack_list_i32(&a1); //序列化 //参数 2 QHash a2 = {{1, "abcde"}}; QByteArray ba2 = QRust_Ser::pack_hi_str(&a2); //序列化 //参数 3 QHash a3 = {{"a", "12345中文"}}; QByteArray ba3 = QRust_Ser::pack_hs_str(&a3); //序列化 rust.call(ba1, ba2, ba3); //调用函数并传参 QHash ret; //声明返回值 QRust_De::upack_hs_str(rust.pop(), &ret); //反序列化获得返回值 ``` 在上面的代码中,声明要调用的Rust端函数foo2,序列化并传递三个参数,函数调用后反序列化获得返回值。在示例中,实现了三种复杂数据类型的转换: | Qt端 | QList | QHash | QHash | |---|---|---|---| | Rust端 | Vec | HashMap | HashMap | #### 更多 [QRust(一) 简介](https://onthessh.com/articles/qrust01/) [QRust(二)数据类型](https://onthessh.com/articles/qrust02/) [QRust(三)编程框架](https://onthessh.com/articles/qrust03/) [QRust(四)示例程序](https://onthessh.com/articles/qrust04/)