rust - 是否有推荐的模式来构建具有嵌套类型的 Flatbuffers?
问题描述
我有一个带有许多嵌套表的flatbuffers模式。在Typescript
flatbuffers 中,我能够让每个表公开一个在创建时to_offset
返回表的函数。WIPOffset
然后我可以将结果分配给更高级别类型的嵌套字段,并且效果很好。
在 Rust flatbuffers 中,我无法完成这项工作,因为它需要多个可变借用,这当然是不允许的。
您有什么方法可以推荐使用像这样的嵌套模式,其中在整个模式中以更高级别的类型使用各种表?
这是我想做的一个粗略的例子:
table A {
f1: string;
}
table B {
fa: A;
f2: uint;
}
table C {
fa: A;
f3: ushort;
}
fn build_a_offset<'a>(fbb: &'a mut FlatBufferBuilder) -> WIPOffset<A<'a>> {
let args = AArgs {...
};
let a = A::create(fbb, &args);
a
}
fn build_b_buf() -> Vec<u8> {
let mut fbb = FlatBufferBuilder::new_with_capacity(1024);
let a_offset = build_a_offset(&mut fbb);
let b_args = BArgs {
a: a_offset,
f2: 30u32,
}
let b = B::create(&mut fbb, f_args);
fbb.finish(b);
fbb.finished_data().to_vec()
}
关于如何正确构建它的任何建议都将非常有帮助。
解决方案
I did find an answer, I was not using lifetimes correctly.
fn build_a_offset<'a>(fbb: &'mut FlatBufferBuilder<'fbb>) -> WIPOffset<A<'a>> {
let args = AArgs {...
};
let a = A::create(fbb, &args);
a
}
fn build_b_buf() -> Vec<u8> {
let mut fbb = FlatBufferBuilder::new_with_capacity(1024);
let a_offset = build_a_offset(&mut fbb);
let b_args = BArgs {
a: a_offset,
f2: 30u32,
}
let b = B::create(&mut fbb, f_args);
fbb.finish(b);
fbb.finished_data().to_vec()
}
I needed to change the signature for the fbb
ref to this:
fbb: &'mut FlatBufferBuilder<'fbb>
It was appending the suffix lifetime instead of the lifetime prepending the reference. This is because a prepended lifetime controls the life of the reference, and what is required is to control the life of the data included in the reference (that is what an appended <'fbb>
does in Rust).
It would be good to add more examples to the tutorial for things like this. I could help at some point if that is something you need.
推荐阅读
- oracle - 外键 ORA-00001 唯一约束上的 SQL 约束冲突
- android - 向 Android XML 中的现有属性添加新属性标志
- python - 为什么在自动化网站登录时定义我的代码会导致它无法正常工作?
- android - 将 FFMpeg 视频转换为 android 可播放视频
- r - 按颜色排序ggplot facet_wrap?
- python - NLP:从关键字生成文本 (NLG)
- python - How to change the y axis of a 2D image?
- javascript - How to handle CORS policy: No 'Access-Control-Allow-Origin' problem
- javascript - 单元测试 UI 相关功能
- python - 使用 SqlAlchemy 访问连接表的所有列值