相关疑难解决方法(0)

由于 *mut u8,异步块创建的 Future 不是 `Send`

我能够继续实现我的异步 udp 服务器。但是,此错误出现两次,因为我的变量数据的类型*mut u8不是Send:

error: future cannot be sent between threads safely
 help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `*mut u8`
note: captured value is not `Send`
Run Code Online (Sandbox Code Playgroud)

和代码(MRE):

use std::error::Error;
use std::time::Duration;
use std::env;
use tokio::net::UdpSocket;
use tokio::{sync::mpsc, task, time}; // 1.4.0
use std::alloc::{alloc, Layout};
use std::mem;
use std::mem::MaybeUninit;
use std::net::SocketAddr;

const UDP_HEADER: usize = 8;
const IP_HEADER: usize = 20;
const AG_HEADER: usize = 4;
const MAX_DATA_LENGTH: usize = (64 …
Run Code Online (Sandbox Code Playgroud)

rust debouncing rust-tokio

7
推荐指数
1
解决办法
1万
查看次数

包含原始指针的结构可以实现发送和FFI安全吗?

我有一个场景,Rust将C调用malloc一个缓冲区并将结果指针存入结构.稍后,struct将被移动到一个线程并传递给一个C函数,该函数会使它变异.

我问题的天真方法看起来像这样(游乐场):

extern crate libc;

use libc::{c_void, malloc, size_t};
use std::thread;

const INITIAL_CAPACITY: size_t = 8;

extern "C" {
    fn mutate(s: *mut Storage);
}

#[repr(C)]
struct Storage {
    #[allow(dead_code)]
    buf: *mut c_void,
    capacity: usize,
}

fn main() {
    let buf = unsafe { malloc(INITIAL_CAPACITY) };
    let mut s = Storage {
        buf: buf,
        capacity: INITIAL_CAPACITY,
    };
    thread::spawn(move || {
        unsafe {
            mutate(&mut s); // mutates s.val, maybe reallocates it, updating s.capacity if so.
        }
    }).join()
        .unwrap(); …
Run Code Online (Sandbox Code Playgroud)

ffi rust

6
推荐指数
1
解决办法
1425
查看次数

标签 统计

rust ×2

debouncing ×1

ffi ×1

rust-tokio ×1