-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for cell_extras
stabilization
#27746
Comments
cc @SimonSapin |
This is the kind of thing (unlike say |
(Ok, thinking a bit more about it, this may not be true. Something like https://github.com/Kimundi/owning-ref-rs/ could, using |
What about making |
I’d like to nominate at least
fn borrow_get<'a>(hashmap: &'a RefCell<HashMap<String, String>>, key: &str)
-> Option<Ref<'a String>> {
let hashmap: = hashmap.borrow();
if hashmap:.contains_key(key) { // Duplicated hash table lookup.
Some(Ref::map(|hashmap| {
hashmap[key] // panic!() for missing key unlikely to be optimized away
}))
} else {
None
}
} It’s probably possible to do it unsafely without the run-time cost with raw pointers. (But still less risky than guessing the memory layout of |
For future reference, here is fully generic use std::cell::{Ref, RefMut};
pub fn ref_filter_map<
T: ?Sized,
U: ?Sized,
F: FnOnce(&T) -> Option<&U>
>(orig: Ref<T>, f: F) -> Option<Ref<U>> {
f(&orig)
.map(|new| new as *const U)
.map(|raw| Ref::map(orig, |_| unsafe { &*raw }))
}
pub fn ref_mut_filter_map<
T: ?Sized,
U: ?Sized,
F: FnOnce(&mut T) -> Option<&mut U>
>(mut orig: RefMut<T>, f: F) -> Option<RefMut<U>> {
f(&mut orig)
.map(|new| new as *mut U)
.map(|raw| RefMut::map(orig, |_| unsafe { &mut *raw }))
} |
🔔 This issue is now entering its cycle final comment period to be handled in 1.8 🔔 We're specifically considering stabilizing |
As just noted in #30834: I've discovered a safety hole in This is not a problem with the It would be possible to provide a similar, but safe, API by defining a separate type for "mapped" mutex guards that can't be used with a |
It could return in the future if it returned a different guard type, which could not be used with Condvar, otherwise it is unsafe as another thread can invalidate an "inner" reference during a Condvar::wait. cc rust-lang#27746
With |
@alexcrichton Certainly we shouldn't stabilize them, and I tend to agree that we should probably remove them and do this in a more considered way. |
If you decide to keep these functions (or at least the one on RwLock I guess), the lifetimes should be removed from the closure like in http://is.gd/vUbM4D (the example in the link above modified for RwLock currently results in use-after-free). |
I need a I went ahead and also demoed a (I believe) safe API for Code: https://github.com/reem/rust-shared-mutex EDIT: Also submitted #31440 to fix the safety hole noted by @whataloadofwhat and update the |
It could return in the future if it returned a different guard type, which could not be used with Condvar, otherwise it is unsafe as another thread can invalidate an "inner" reference during a Condvar::wait. cc #27746
The libs team discussed this during triage yesterday and the decision was to:
|
This commit is the result of the FCPs ending for the 1.8 release cycle for both the libs and the lang suteams. The full list of changes are: Stabilized * `braced_empty_structs` * `augmented_assignments` * `str::encode_utf16` - renamed from `utf16_units` * `str::EncodeUtf16` - renamed from `Utf16Units` * `Ref::map` * `RefMut::map` * `ptr::drop_in_place` * `time::Instant` * `time::SystemTime` * `{Instant,SystemTime}::now` * `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier` * `{Instant,SystemTime}::elapsed` * Various `Add`/`Sub` impls for `Time` and `SystemTime` * `SystemTimeError` * `SystemTimeError::duration` * Various impls for `SystemTimeError` * `UNIX_EPOCH` * `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign` Deprecated * Scoped TLS (the `scoped_thread_local!` macro) * `Ref::filter_map` * `RefMut::filter_map` * `RwLockReadGuard::map` * `RwLockWriteGuard::map` * `Condvar::wait_timeout_with` Closes rust-lang#27714 Closes rust-lang#27715 Closes rust-lang#27746 Closes rust-lang#27748 Closes rust-lang#27908 Closes rust-lang#29866
This commit is the result of the FCPs ending for the 1.8 release cycle for both the libs and the lang suteams. The full list of changes are: Stabilized * `braced_empty_structs` * `augmented_assignments` * `str::encode_utf16` - renamed from `utf16_units` * `str::EncodeUtf16` - renamed from `Utf16Units` * `Ref::map` * `RefMut::map` * `ptr::drop_in_place` * `time::Instant` * `time::SystemTime` * `{Instant,SystemTime}::now` * `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier` * `{Instant,SystemTime}::elapsed` * Various `Add`/`Sub` impls for `Time` and `SystemTime` * `SystemTimeError` * `SystemTimeError::duration` * Various impls for `SystemTimeError` * `UNIX_EPOCH` * `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign` Deprecated * Scoped TLS (the `scoped_thread_local!` macro) * `Ref::filter_map` * `RefMut::filter_map` * `RwLockReadGuard::map` * `RwLockWriteGuard::map` * `Condvar::wait_timeout_with` Closes #27714 Closes #27715 Closes #27746 Closes #27748 Closes #27908 Closes #29866 Closes #28235 Closes #29720
Reopening as apparently |
Also cc #33880, possible unsoundness with that method. |
@rfcbot fcp merge Sounds like |
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 psst @alexcrichton, I wasn't able to add the |
The final comment period is now complete. |
Library stabilizations/deprecations for 1.15 release Stabilized: - `std::iter::Iterator::{min_by, max_by}` - `std::os::*::fs::FileExt` - `std::sync::atomic::Atomic*::{get_mut, into_inner}` - `std::vec::IntoIter::{as_slice, as_mut_slice}` - `std::sync::mpsc::Receiver::try_iter` - `std::os::unix::process::CommandExt::before_exec` - `std::rc::Rc::{strong_count, weak_count}` - `std::sync::Arc::{strong_count, weak_count}` - `std::char::{encode_utf8, encode_utf16}` - `std::cell::Ref::clone` - `std::io::Take::into_inner` Deprecated: - `std::rc::Rc::{would_unwrap, is_unique}` - `std::cell::RefCell::borrow_state` Closes #23755 Closes #27733 Closes #27746 Closes #27784 Closes #28356 Closes #31398 Closes #34931 Closes #35601 Closes #35603 Closes #35918 Closes #36105
This commit is the result of the FCPs ending for the 1.8 release cycle for both the libs and the lang suteams. The full list of changes are: Stabilized * `braced_empty_structs` * `augmented_assignments` * `str::encode_utf16` - renamed from `utf16_units` * `str::EncodeUtf16` - renamed from `Utf16Units` * `Ref::map` * `RefMut::map` * `ptr::drop_in_place` * `time::Instant` * `time::SystemTime` * `{Instant,SystemTime}::now` * `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier` * `{Instant,SystemTime}::elapsed` * Various `Add`/`Sub` impls for `Time` and `SystemTime` * `SystemTimeError` * `SystemTimeError::duration` * Various impls for `SystemTimeError` * `UNIX_EPOCH` * `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign` Deprecated * Scoped TLS (the `scoped_thread_local!` macro) * `Ref::filter_map` * `RefMut::filter_map` * `RwLockReadGuard::map` * `RwLockWriteGuard::map` * `Condvar::wait_timeout_with` Closes rust-lang#27714 Closes rust-lang#27715 Closes rust-lang#27746 Closes rust-lang#27748 Closes rust-lang#27908 Closes rust-lang#29866
The
Ref
type connected toRefCell
supports some methods for changing the type of the reference, such asmap
andfilter_map
. Whilemap
is clearly fully general,filter_map
is somewhat of a special case hooked intoOption
. To make it fully general would likely require HKT. We need an overall plan before stabilization.The text was updated successfully, but these errors were encountered: