- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 C 库,它有 gpio 实现。 gpio_type 是目标特定的,每个 MCU 对 gpio_type 都有不同的定义。库中的功能之一:
void gpio_init(gpio_type *object, int32_t pin);
我想使用 C 库函数在 Rust 中编写 Gpio 对象的抽象。因此需要类似不透明指针类型的东西(在 C++ 中,我只会创建一个类型为:gpio_type 的成员变量)。我想我会创建一个空枚举(或结构),为对象分配所需的空间并将其转换为匹配 C 层中的类型。
pub enum gpio_type {}
#[link(name = "gpio_lib", kind = "static")]
extern {
pub fn gpio_init(obj: *mut gpio_type, value: i32);
}
pub struct Gpio {
gpio : *mut gpio_type,
}
impl Gpio {
pub fn new(pin: u32) -> Gpio {
unsafe {
let mut gpio_ptr : &'static [u8; 4] = init(); // size of gpio in C is 4 bytes for one target, will be changed later to obtain it dynamically
let gpio_out = Gpio { gpio: transmute(gpio_ptr)};
gpio_init(gpio_out.gpio, pin);
gpio_out
}
}
}
这针对嵌入式设备,因此没有 std,没有 libc。我不想为 rust 中的每个目标重新定义 gpio_type(为每个目标复制 C 声明),寻找一些东西来为 C 将处理的对象分配内存。
下面的代码片段根据反汇编生成指向地址 0 的指针。 Gpio新方法反汇编:
45c: b580 push {r7, lr}
45e: 466f mov r7, sp
460: 4601 mov r1, r0
462: 2000 movs r0, #0
464: f000 fae6 bl a34 <gpio_init>
468: 2000 movs r0, #0
46a: bd80 pop {r7, pc}
知道为什么 462 是 0 吗?
最佳答案
looking for something to just allocate memory for the object which C will handle
像这样的事情呢?给结构一个实际大小(在这种情况下,通过给它一个固定大小的字节大小的项目数组),在堆上分配该空间,然后将其视为原始指针。
use std::mem;
#[allow(missing_copy_implementations)]
pub struct Gpio([u8; 4]);
impl Gpio {
fn new() -> Gpio { Gpio([0,0,0,0]) }
}
fn main() {
// Allocate some bytes and get a raw pointer
let a: *mut u8 = unsafe { mem::transmute(Box::new(Gpio::new())) };
// Use it here!
// When done... back to a box
let b: Box<Gpio> = unsafe { mem::transmute(a) };
// Now it will be dropped automatically (and free the allocated memory)
// Or you can be explicit
drop(b);
}
但是,我建议这样做;它更明显,不需要堆分配:
#[allow(missing_copy_implementations)]
pub struct Gpio([u8; 4]);
impl Gpio {
fn new() -> Gpio { Gpio([0,0,0,0]) }
fn as_mut_ptr(&mut self) -> *mut u8 {
self.0.as_mut_ptr()
}
}
fn main() {
let mut g = Gpio::new();
let b = g.as_mut_ptr();
}
作为奖励,您可以获得一个很好的地方来挂起一些方法。可能 as_mut_ptr
不需要公开,并且可以隐藏在 Gpio
结构的公共(public)方法后面。
(也可以使用 uninitialized
代替 [0,0,0,0]
)
// This depends on your library, check the FFI guide for details
extern {
fn gpio_init(gpio: *mut u8, pin: u8);
fn gpio_pin_on(gpio: *mut u8);
fn gpio_pin_off(gpio: *mut u8);
}
#[allow(missing_copy_implementations)]
pub struct Gpio([u8; 4]);
impl Gpio {
fn new(pin: u8) -> Gpio {
let mut g = Gpio([0,0,0,0]);
g.init(pin);
g
}
fn as_mut_ptr(&mut self) -> *mut u8 {
self.0.as_mut_ptr()
}
fn init(&mut self, pin: u8) { unsafe { gpio_init(self.as_mut_ptr(), pin) } }
pub fn on(&mut self) { unsafe { gpio_pin_on(self.as_mut_ptr()) } }
pub fn off(&mut self) { unsafe { gpio_pin_off(self.as_mut_ptr()) } }
}
static BLUE_LED_PIN: u8 = 0x4;
fn main() {
let mut g = Gpio::new(BLUE_LED_PIN);
g.on();
g.off();
}
关于c - 为 C/FFI 库调用分配对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154683/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!