Idea : Explicit And More Flexible Lifetime Annotation And Thread's At Exit Hook
The problem I am trying to solve is the complexity of the current lifetime annotation, that makes everyone will just avoid it so Rust in reality is not like Rust in paper
For example :
struct DatabaseConfig<'a, 'b> {
host: &'a str,
query_cache: &'b [u8],
}
struct NetworkConfig<'c> {
bind_address: &'c str,
}
struct Server<'a, 'b, 'c> {
db: DatabaseConfig<'a, 'b>,
net: NetworkConfig<'c>,
}
struct App<'a, 'b, 'c> {
server: Server<'a, 'b, 'c>,
}
impl<'a, 'b, 'c> App<'a, 'b, 'c> {
fn run(&self) {
println!("{}", self.server.db.host);
}
}
That code is scary and confusing, all the requirement 'a, 'b, 'c will keep spread to the other code that call them
Then, it become like this :
struct DatabaseConfig {
host: &str,
query_cache: &[u8],
}
struct NetworkConfig {
bind_address: &str,
}
struct Server {
db: DatabaseConfig,
net: NetworkConfig,
}
struct App {
server: Server,
}
impl App {
fn run(&self) {
println!("DB Host: {}", self.server.db.host);
println!("Net Bind: {}", self.server.net.bind_address);
}
}
// simulating the use of borrow
fn main() {
let host_data = String::from("localhost") until MyLifetime;
let cache_data = vec![1, 2, 3] until MyLifetime;
let bind_data = String::from("0.0.0.0") until MyLifetime;
let app = App {
server: Server {
db: DatabaseConfig {
host: &host_data,
query_cache: &cache_data
},
net: NetworkConfig {
bind_address: &bind_data
},
}
} until MyLifetime;
app.run();
close MyLifetime;
}
For the Thread at_exit hook, the problem it solves is the limitation of `std::thread::scope` that force to wait in place where it is written, with the cons can't use reference to stack data, but only using reference to heap data. It removes the usual need of Arc if want to share heap data to other 1 thread