-
Notifications
You must be signed in to change notification settings - Fork 23
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
Default and/or Insert, onboarding. #18
Comments
@cheako Similar to what's discussed here: hwchen/keyring-rs#103 You can create a helper function: /// Helper to obtain the value, returning None when not
/// yet set.
pub fn get_optional<T: DeserializeOwned>(
store: &PkvStore,
value: &str,
) -> Result<Option<T>, Box<dyn Error>> {
match store.get(value) {
Ok(s) => Ok(Some(s)),
Err(bevy_pkv::GetError::NotFound) => Ok(None),
Err(e) => Err(e),
}
}
let h: PkvStore;
let _limits: Option<Limits> = get_optional(&h, "limits")?;
let _old_boot_id: Option<u64> = get_optional(&h, "boot_id")?; |
Looks good, what if that was a pub function in the crate? That would prevent almost every client from having something similar. |
I'd like to try to keep simple usage as ergonomic as possible, but that what's simple may depend on your intention/expectation when using the store. Sometimes you really do expect the thing you query for to be there, so having a Adding a such For reference, in bevy APIs, the approach to this is having some methods often come in two flavors, for instance:
|
The first time an application is run, using a default value appears clunky. Catching the
NotFound
Error is natural enough to have specific helpers for that case. There is also an argument that this is not an error andget
should return anResult<Option<_>...
sounds interesting.Notice the examples are bad(delete those b4 someone copies them), they just roll through any errors by treating any error as the
NotFound
condition.Similar to: hwchen/keyring-rs#103
It would be nice if there were rust conventions surrounding these APIs, if you look at
std
they almost always returns anOption
... noting the difference that IO errors are not a thing forHashMap
.I think there are better ways to write this, suggestions welcome.
The text was updated successfully, but these errors were encountered: