rust - 如何获取可变 u32 指针并将其转换为 C 的 int 指针
问题描述
假设我有一个 C 函数:
void func(char *buf, unsigned int *len);
为了在 Rust 中调用它,我声明:
pub fn func(buf: *mut ::std::os::raw::c_char, len: *mut ::std::os::raw::c_uint) {
unimplemented!()
}
然后我写了另一个包装器:
pub fn another_func() -> String {
let mut capacity: u32 = 256;
let mut vec = Vec::with_capacity(capacity as usize);
unsafe {
func(vec.as_ptr() as *mut c_char, &capacity as *mut c_uint)
};
String::from_utf8(vec).unwrap();
unimplemented!()
}
但是编译器告诉我:
error[E0606]: casting `&u32` as `*mut u32` is invalid
--> src/main.rs:...:28
|
307 | &capacity as *mut c_uint)
为什么我不能投capacity
进去*mut c_unit
?
解决方案
我必须使引用可变。
func(vec.as_ptr() as *mut c_char, &mut capacity as *mut c_uint)
推荐阅读
- c# - ElasticSearch 6.0.1 - SQL DISTINCT 子句 - NEST C#
- c++ - HDF5:如何将紧密排列的顶点数组读入不同大小的结构?
- r - 更改箱线图中的标签(不是轴)
- git - 远程 git repo 或存档错误:无法读取未定义兰多的属性“拆分”
- kubernetes - JupyterHub generic.oauthenticator:500 内部服务器错误/cookie 中缺少 400 OAuth 状态
- sql - How to insert values in already created SQLite file in Android Studio?
- python - 为什么不以这种方式使用 for 循环替换列表中的每个项目?
- java - Jackson - 嵌套映射值的自定义反序列化器
- php - 如何使用 htaccess 和 php 避免错误变量
- c# - 图像 src 指向 WEB FORM 中我的项目文件夹之外的文件