-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add borrow_with to LazyCell #56
Conversation
Also, sorry, I really meant to open this pr against my fork, because I actually have not gotten the chance to go over the contributing.md yet. :( |
5a34cdc
to
0540b51
Compare
No worries :) Once you've read through the CONTRIBUTING.md and got the PR up to standard, just ping me and I'll review it. |
@indiv0 Actually I think it's good now... Looked like I just needed to fix the commit message? Be happy to fix anything else that comes up. |
/// If the underlying value of the cell has not yet been realized, the | ||
/// cell is first filled using the function provided. | ||
pub fn borrow_with<F: FnOnce() -> T>(&self, f: F) -> &T { | ||
let mut slot = unsafe {&mut *self.inner.get() }; |
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.
Please add a space before &mut
(i.e. { &mut
). Picky, I know.
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.
No, not picky at all--that's an embarrassing oversight on my part. :p
0540b51
to
538226d
Compare
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.
Sorry for the very nitpicky review. Overall I agree it and I think it'll be a good addition to the API.
@@ -85,6 +85,21 @@ impl<T> LazyCell<T> { | |||
unsafe { &*self.inner.get() }.as_ref() | |||
} | |||
|
|||
/// Borrows the contents of this lazy cell for the duration of the cell | |||
/// itself |
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.
Please add a period at the end of the sentence.
/// Borrows the contents of this lazy cell for the duration of the cell | ||
/// itself | ||
/// | ||
/// If the underlying value of the cell has not yet been realized, the |
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.
Could you replace this sentence with: "If the cell has not yet been filled, the cell is first filled using the function provided"
let lazycell = LazyCell::new(); | ||
|
||
let value = lazycell.borrow_with(|| 1); | ||
assert_eq!(&1, value); |
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.
Could you also test the positive case (i.e. borrow_with(|| 1)
on a cell that's already been filled (just for complete test coverage)).
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.
Yes, I'll make a note to write that up. I was also thinking a test to assert that the || filler_func
only gets called once would be appropriate.
} | ||
|
||
*slot = Some(f()); | ||
slot.as_ref().unwrap() |
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.
Could you rewrite the function to be:
...
if !slot.is_some() {
*slot = Some(f());
}
slot.as_ref().unwrap()
@indiv0 Do you have any idea why coveralls is complaining about this? I guess I may not understand how the coverage test is intended to work, but it seems weird that it would go down so much--seems like the new code would be covered? oO |
Coveralls is complaining so much (I believe) because Also, don't forget to add yourself to the |
538226d
to
8af5eb9
Compare
Add a `borrow_with(|| foo)` convenience method to `LazyCell` so that users may provide a function to automatically fill the cell in the event that it is empty on attempting to borrow.
8af5eb9
to
a15efa3
Compare
@indiv0 Looks like you were right about that coverage--and I think I now have a better understanding of what it's measuring. Don't worry about the review! Not nitpicky at all. :) Was just thinking that I did not update the version in |
sigh coveralls is still being weird. But that's fine. Updating the version and making a release must be done manually, unfortunately, but that's something I'll handle on my end, right now. Thank you very much for this PR! |
Actually I think I figured out the source of the remaining weirdness. Coveralls is being run for both Linux and OS X builds, and they're giving differing results. I'll probably disable coveralls on one to avoid this. |
Either way, v0.5.0 is published. Thanks again! https://github.com/indiv0/lazycell/releases/tag/v0.5.0 |
Pursuant to some commentary from https://www.reddit.com/r/rust/comments/5gv3k9/lazy_fields, this adds
.borrow_with()
toLazyCell
only. I tinkered with adding it to the atomic version, but it seemed to me the api would not be as clean there and, as a result, not nearly as useful.