Skip to content
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

feat: add return val to replace_bucket_with replace_entry_with #419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Apr 12, 2023

  1. feat: add return val to replace_bucket_with replace_entry_with

    If a user has a `RawOccupiedEntryMut`, they should be able to use `replace_entry_with` to conditionally remove it. However, if a user wishes to extract data from this entity during the removal process, the current api necessitates the use of `unwrap`:
    
    ```rust
    /// Extract the coolness of an item, possibly destroying it
    fn extract_coolness<V>(v: V) -> (Option<V>, Coolness) {
        /// ...
    }
    
    let entry: RawOccupiedEntryMut< _, _, _, _> = unimplemented!();
    
    let mut coolness = None;
    let entry = entry.replace_entry_with(|_k, v| {
        let (maybe_v, rcool) = extract_coolness(v);
        coolness = Some(rcool);
        maybe_v
    });
    let coolness = coolness.unwrap(); // unwrap, ew
    
    // Do some stuff with the coolness it doesn't really matter what
    inspect_coolness(coolness);
    ```
    
    This is because while `F` is bounded by `FnOnce`, it is under no obligation to call it, which in this example means there is no guarnatee that `coolness` is initialized. This proposed change introduces a new return value `R` which allows callers to transport this data out of `F`. Notably it also commits the `replace_entry_with` to calling `F`, since there is no other way to generate an `R`. With this, our example becomes:
    
    ```rust
    /// Extract the coolness of an item, possibly destroying it
    fn extract_coolness<V>(v: V) -> (Option<V>, Coolness) {
        /// ...
    }
    
    let entry: RawOccupiedEntryMut< _, _, _, _> = unimplemented!();
    
    let (entry, coolness) = entry.replace_entry_with(|_k, v| extract_coolness(v));
    
    // Do some stuff with the coolness it doesn't really matter what
    inspect_coolness(coolness);
    ```
    Daniel-Aaron-Bloom committed Apr 12, 2023
    Configuration menu
    Copy the full SHA
    9c6036e View commit details
    Browse the repository at this point in the history