-
Notifications
You must be signed in to change notification settings - Fork 315
Update CH-14, Memory Management to add LazyCell and LazyLock #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Output for |
|
OK, so ideally we would have an example that demonstrates using LazyLock to solve a real problem, not to print. Here's an example I'd rather showcase Deferring Expensive Computations in a Struct use std::cell::LazyCell;
struct User {
id: u32,
username: String,
// We defer the expensive permission calculation
permissions: LazyCell<Vec<String>>,
}
impl User {
fn new(id: u32, username: String) -> Self {
Self {
id,
username,
permissions: LazyCell::new(|| {
println!("--- Fetching permissions from database for ID {} ---", id);
// Simulate a heavy operation
vec!["read".to_string(), "write".to_string()]
}),
}
}
}
fn main() {
let user = User::new(1, "ferris_rustacean".into());
println!("User {} loaded.", user.username);
// If we never access user.permissions, the closure is never run.
if true { // Imagine a conditional check here
println!("Permissions: {:?}", *user.permissions);
}
} |
|
Oh I see you already upated, I realized I made a mistake and provided 2 LazyLock examples |
82e806f to
7706a93
Compare
|
I updated both the examples cc: @AndyGauge |
AndyGauge
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally the descriptions are bit more explanative, but the comments do the job.
|
Thanks for the report on the links, I'll try to update them and the tests to get a deployment done by the end of the month. Thank you for your contribution! |
fixes #744
Things to check before submitting a PR
cargo xtask test all(I'm pretty sure the link test fails for an unrelated reason)https://docs.rs/tar/*/tar/struct.Entry.htmlThings to do after submitting PR
Thank you for reading, you may now delete this text! Thank you! 😄