我有一个值,我想在我自己的类型中存储该值以及对该值内部内容的引用:
struct Thing {
count: u32,
}
struct Combined<'a>(Thing, &'a u32);
fn make_combined<'a>() -> Combined<'a> {
let thing = Thing { count: 42 };
Combined(thing, &thing.count)
}
Run Code Online (Sandbox Code Playgroud)
有时候,我有一个值,我想在同一个结构中存储该值和对该值的引用:
struct Combined<'a>(Thing, &'a Thing);
fn make_combined<'a>() -> Combined<'a> {
let thing = Thing::new();
Combined(thing, &thing)
}
Run Code Online (Sandbox Code Playgroud)
有时,我甚至没有参考该值,我得到同样的错误:
struct Combined<'a>(Parent, Child<'a>);
fn make_combined<'a>() -> Combined<'a> {
let parent = Parent::new();
let child = parent.child();
Combined(parent, child)
}
Run Code Online (Sandbox Code Playgroud)
在每种情况下,我都会收到一个错误,即其中一个值"活不够长".这个错误是什么意思?
我试图使用蚂蚁内存分析器找到内存泄漏,我在一个新术语中遇到过:
固定物体.
有人可以给我一个关于这个对象是什么的简单而简单的解释,我如何pinn/Unpinn对象,并检测谁固定对象?
谢谢
我有使用数据调用 Rust 代码的 C++ 代码。它知道将数据发送到哪个对象。下面是 C++ 回调的 Rust 函数示例:
extern "C" fn on_open_vpn_receive(
instance: Box<OpenVpn>,
data: *mut c_uchar,
size: *mut size_t,
) -> u8
Run Code Online (Sandbox Code Playgroud)
它将指针作为 a 接收Box,因此我创建了一个函数openvpn_set_rust_parent来设置 C++ 必须回调的对象。这个对象是一个指向自身的指针。我正在使用,Pin因此Box不会重新分配到其他地方,从而使 C++ 调用无效地址。
impl OpenVpn {
pub fn new() -> Pin<Box<OpenVpn>> {
let instance = unsafe { interface::openvpn_new(profile.as_ptr()) };
let o = OpenVpn { instance: instance };
let p = Box::pin(o);
unsafe {
interface::openvpn_set_rust_parent(o.instance, p.as_ptr());
};
p
}
}
Run Code Online (Sandbox Code Playgroud)
签名:
pub fn openvpn_set_rust_parent(instance: *mut …Run Code Online (Sandbox Code Playgroud) 我有一个异步Stream,我想从中获取第一个值。我怎样才能这样做呢?
use futures::Stream; // 0.3.5
async fn example<T>(s: impl Stream<Item = T>) -> Option<T> {
todo!("What goes here?")
}
Run Code Online (Sandbox Code Playgroud) rust ×3
async-await ×1
asynchronous ×1
c# ×1
ffi ×1
future ×1
lifetime ×1
memory ×1
rust-pin ×1
stream ×1