2087 shaares
6 private links
6 private links
77 results
tagged
rust
/// The idea of this function is convert from a Vec of some time to an empty Vec of another type, reusing the heap allocation
fn reuse_vec<T, U>(mut v: Vec<T>) -> Vec<U> {
const {
assert!(size_of::<T>() == size_of::<U>());
assert!(align_of::<T>() == align_of::<U>());
}
v.clear();
v.into_iter().map(|_| unreachable!()).collect()
}
pub struct Foo<'a> {
owned: String,
borrowed: &'a str,
}
struct StaticFoo {
owned: String,
borrowed: MaybeUninit<&'static str>,
}
fn without_lifetime(foos: Vec<Foo>) -> Vec<StaticFoo> {
foos.into_iter()
.map(|f| StaticFoo {
owned: f.owned,
borrowed: MaybeUninit::uninit(),
})
.collect()
}
// The presence of MaybeUnit::uninit() tells the compiler that it’s OK to have anything there, so it can choose to leave whatever &str was in the original Foo struct. This means that it’s valid to produce a StaticFoo with the same in-memory representation as the Foo that it replaces, allowing it to eliminate the loop
use axum::{routing::get, Router};
use tokio::net::TcpListener;
async fn index() -> &'static str {
"Hello, World!"
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app = Router::new().route("/", get(index));
let mut listenfd = listenfd::ListenFd::from_env();
let listener = match listenfd.take_tcp_listener(0)? {
Some(listener) => TcpListener::from_std(listener),
None => TcpListener::bind("0.0.0.0:3000").await,
}?;
axum::serve(listener, app).await?;
Ok(())
}
$ systemfd --no-pid -s http::5555 -- watchexec -r -- cargo run
~> socket http://127.0.0.1:5555/ -> fd #3
[Running: cargo run]
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/axum-test`
[Running: cargo run]
Compiling axum-test v0.1.0 (/private/tmp/axum-test)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.52s
Running `target/debug/axum-test`
union CellValue {
tag: TaggedPtr<Aligned, 2>,
num: Decimal,
str: ManuallyDrop<ThinVec<u8>>,
formula: ManuallyDrop<Rc<Formula>>,
iter: ManuallyDrop<Box<dyn Iterator<Item = CellValue>>>,
}
struct UsersService {
config: &'static Config,
}
struct OrdersService {
config: &'static Config,
}
// ...
fn load() -> Result<&'static Config, Error> {
// ...
return Ok(Box::leak(Box::new(config)));
}
// ...
let config = config::load()?;
let users_service = UsersService::new(config);
let orders_service = OrdersService::new(config);
User space scheduling