diff options
| author | Jonas Schievink <jonasschievink@gmail.com> | 2020-10-24 14:11:57 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-24 14:11:57 +0200 |
| commit | d7c635b3a514159afd3a61064772ec17cc1c44c2 (patch) | |
| tree | 80ab721a22e877950e86e48ec04dffdfd2f496d1 /compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | |
| parent | 8e756698dfc6219a0acf511a9ebb9e6063bdadc2 (diff) | |
| parent | 216d0fe36466ce9307a643a67afa41ebfb8c43dd (diff) | |
| download | rust-d7c635b3a514159afd3a61064772ec17cc1c44c2.tar.gz rust-d7c635b3a514159afd3a61064772ec17cc1c44c2.zip | |
Rollup merge of #77392 - Canop:option_insert, r=m-ou-se
add `insert` to `Option`
This removes a cause of `unwrap` and code complexity.
This allows replacing
```
option_value = Some(build());
option_value.as_mut().unwrap()
```
with
```
option_value.insert(build())
```
It's also useful in contexts not requiring the mutability of the reference.
Here's a typical cache example:
```
let checked_cache = cache.as_ref().filter(|e| e.is_valid());
let content = match checked_cache {
Some(e) => &e.content,
None => {
cache = Some(compute_cache_entry());
// unwrap is OK because we just filled the option
&cache.as_ref().unwrap().content
}
};
```
It can be changed into
```
let checked_cache = cache.as_ref().filter(|e| e.is_valid());
let content = match checked_cache {
Some(e) => &e.content,
None => &cache.insert(compute_cache_entry()).content,
};
```
*(edited: I removed `insert_with`)*
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp')
0 files changed, 0 insertions, 0 deletions
