ref_filter_map-1.0.1/lib.rs00006440001750000175000000006414126673162030014036 0ustar0000000000000000//! The `Ref` and `RefMut` types in `std::cell` each have a `map` method that create //! a new `Ref` (`RefMut`) that borrows something (a sub-component) inside of a `RefCell`. //! //! When that component may or may not be there, //! you may find yourself checking for its precense twice: //! //! ``` //! # use std::cell::{RefCell, Ref}; //! # use std::collections::HashMap; //! fn borrow_get<'a>(hashmap: &'a RefCell>, key: &str) //! -> Option> { //! let hashmap = hashmap.borrow(); //! if hashmap.contains_key(key) { // Duplicated hash table lookup. //! Some(Ref::map(hashmap, |hashmap| { //! &hashmap[key] // panic!() for missing key unlikely to be optimized away //! })) //! } else { //! None //! } //! } //! ``` //! //! This crate define `ref_filter_map` and `ref_mut_filter_map` functions //! that are a lot like `Ref::map` and `RefMut::map`, //! but return `Option` and take closures that return `Option`. //! //! Internally they use a raw pointer and some `unsafe` code, //! but the API they provide is believed to be safe. //! //! This was once part of `std::cell` but has been deprecated there since it makes `Option` //! too much of a special case. //! //! https://github.com/rust-lang/rust/pull/25747 //! https://github.com/rust-lang/rust/issues/27746 use std::cell::{Ref, RefMut}; /// Make a new `Ref` for a optional component of the borrowed data, e.g. an enum variant. /// /// The `RefCell` is already immutably borrowed, so this cannot fail. /// /// This is an associated function that needs to be used as `Ref::filter_map(...)`. /// A method would interfere with methods of the same name on the contents of a `RefCell` /// used through `Deref`. /// /// # Example /// /// ``` /// use std::cell::{RefCell, Ref}; /// use ref_filter_map::ref_filter_map; /// /// let c = RefCell::new(Ok(5)); /// let b1: Ref> = c.borrow(); /// let b2: Ref = ref_filter_map(b1, |o| o.as_ref().ok()).unwrap(); /// assert_eq!(*b2, 5) /// ``` pub fn ref_filter_map< T: ?Sized, U: ?Sized, F: FnOnce(&T) -> Option<&U> >(orig: Ref, f: F) -> Option> { f(&orig) .map(|new| new as *const U) .map(|raw| Ref::map(orig, |_| unsafe { &*raw })) } /// Make a new `RefMut` for a optional component of the borrowed data, e.g. an enum variant. /// /// The `RefCell` is already mutably borrowed, so this cannot fail. /// /// This is an associated function that needs to be used as `RefMut::filter_map(...)`. /// A method would interfere with methods of the same name on the contents of a `RefCell` /// used through `Deref`. /// /// # Example /// /// ``` /// use std::cell::{RefCell, RefMut}; /// use ref_filter_map::ref_mut_filter_map; /// /// let c = RefCell::new(Ok(5)); /// { /// let b1: RefMut> = c.borrow_mut(); /// let mut b2: RefMut = ref_mut_filter_map(b1, |o| o.as_mut().ok()).unwrap(); /// assert_eq!(*b2, 5); /// *b2 = 42; /// } /// assert_eq!(*c.borrow(), Ok(42)); /// ``` pub fn ref_mut_filter_map< T: ?Sized, U: ?Sized, F: FnOnce(&mut T) -> Option<&mut U> >(mut orig: RefMut, f: F) -> Option> { f(&mut orig) .map(|new| new as *mut U) .map(|raw| RefMut::map(orig, |_| unsafe { &mut *raw })) } ref_filter_map-1.0.1/Cargo.toml00006440001750000175000000001400126673164070014646 0ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "ref_filter_map" version = "1.0.1" authors = ["Simon Sapin "] description = "Like `std::cell::{Ref,RefMut}::map`, but for optional components." license = "MIT OR Apache-2.0" repository = "https://github.com/SimonSapin/rust-std-candidates" [lib] path = "lib.rs" ref_filter_map-1.0.1/Cargo.toml.orig00006440001750000175000000000455126673164070015616 0ustar0000000000000000[package] name = "ref_filter_map" version = "1.0.1" authors = ["Simon Sapin "] license = "MIT OR Apache-2.0" repository = "https://github.com/SimonSapin/rust-std-candidates" description = "Like `std::cell::{Ref,RefMut}::map`, but for optional components." [lib] path = "lib.rs"