Streaming Rust

Rust さんぽ

shard rocks db

Other tips

channels

std::mpsc::Sender is !Sync (which means you need to wrap it with a Mutex or similar) where crossbeam::channel::Sender is Sync. Other than the perforance gains, seems like crossbeam is status quo.

accessing the tokio’s runtime from other threads

we don’t want multiple runtimes in one app.

use std::thread;

#[tokio::main]
async fn main() {
    thread::spawn(move ||{
        async_function().await;
    });
}

async fn async_function() {
}

but there might be situations that we want to fun async functions inside our dedicated thread.

in that case you get the handle of the runtime and use that

use tokio::runtime::Handle;

let handle = Handle::current();

handle.spawn(async move{
    async_function().await;
});

other small things

const or static

Almost always use const. I guess when const it lives in the binary, on the otherhand when it’s a static it has a memory assigned and could be modified through unsafe operations.

https://stackoverflow.com/questions/52751597/what-is-the-difference-between-a-constant-and-a-static-variable-and-which-should

Date: 2023-08-03 Thu 14:08