From 5a57b2851b4883de09e974f13563a62eec5d26cf Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Tue, 15 Jun 2021 22:17:23 +0000 Subject: [PATCH] Bug 1716518 - Upgrade owning_ref to v0.4.1. r=emilio Differential Revision: https://phabricator.services.mozilla.com/D117826 --- Cargo.lock | 4 +- .../rust/owning_ref/.cargo-checksum.json | 2 +- third_party/rust/owning_ref/CHANGELOG.md | 8 + third_party/rust/owning_ref/Cargo.toml | 4 +- third_party/rust/owning_ref/README.md | 21 +-- third_party/rust/owning_ref/src/lib.rs | 176 ++++++++++++++---- 6 files changed, 159 insertions(+), 56 deletions(-) create mode 100644 third_party/rust/owning_ref/CHANGELOG.md diff --git a/Cargo.lock b/Cargo.lock index e59464e28416..2a99846276d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3648,9 +3648,9 @@ dependencies = [ [[package]] name = "owning_ref" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" +checksum = "6ff55baddef9e4ad00f88b6c743a2a8062d4c6ade126c2a528644b8e444d52ce" dependencies = [ "stable_deref_trait", ] diff --git a/third_party/rust/owning_ref/.cargo-checksum.json b/third_party/rust/owning_ref/.cargo-checksum.json index 636230fd7342..274171be1595 100644 --- a/third_party/rust/owning_ref/.cargo-checksum.json +++ b/third_party/rust/owning_ref/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"9cc8936f2504375b24b88060b766a908e69690246cd063138b3a9b39287640d7","LICENSE":"90bc15ed094593083fd129fdd1a03607be80fe8839c5564616a5961ab7f7a194","README.md":"a479bb465878f3cbb8d53d2f42a26ac309e121d7c635956b7547a6bdfedce5d4","src/lib.rs":"e1e523638bea245fcf00b6643c61f856f2404f4d5d9ed0c230a6c4d2d8550b1c"},"package":"49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"} \ No newline at end of file +{"files":{"CHANGELOG.md":"9a3880e4d5435ca03c0197c45d4ce6b6db6d3fb7eda855f297bafc2ec28af836","Cargo.toml":"91184e6b143ddf621fe7e6757fb28d8eb4c0e01f541fbd0dc440bf076ebfd20d","LICENSE":"90bc15ed094593083fd129fdd1a03607be80fe8839c5564616a5961ab7f7a194","README.md":"0dfea39ad1662ed0a61bb1453eafc207a53dfe577e7846c87d3a5161574fdb12","src/lib.rs":"2e423e210bf2e3ee5e4c426b9a3c9b7756fc434beff1ccf9afa48704e4ac993d"},"package":"6ff55baddef9e4ad00f88b6c743a2a8062d4c6ade126c2a528644b8e444d52ce"} \ No newline at end of file diff --git a/third_party/rust/owning_ref/CHANGELOG.md b/third_party/rust/owning_ref/CHANGELOG.md new file mode 100644 index 000000000000..a5d24e8c82fb --- /dev/null +++ b/third_party/rust/owning_ref/CHANGELOG.md @@ -0,0 +1,8 @@ +# Changelog + +## [0.4.1] - 2020-02-27 +### Added +- `map_with_owner` (#51) + +### Changed +- Use dyn for trait objects (#53) diff --git a/third_party/rust/owning_ref/Cargo.toml b/third_party/rust/owning_ref/Cargo.toml index 4ef64eaa01fb..057fd5c1bb96 100644 --- a/third_party/rust/owning_ref/Cargo.toml +++ b/third_party/rust/owning_ref/Cargo.toml @@ -3,7 +3,7 @@ # 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 +# to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're @@ -12,7 +12,7 @@ [package] name = "owning_ref" -version = "0.4.0" +version = "0.4.1" authors = ["Marvin Löbel "] description = "A library for creating references that carry their owner with them." documentation = "http://kimundi.github.io/owning-ref-rs/owning_ref/index.html" diff --git a/third_party/rust/owning_ref/README.md b/third_party/rust/owning_ref/README.md index cd14b2eb09d0..ad32ac24fd3b 100644 --- a/third_party/rust/owning_ref/README.md +++ b/third_party/rust/owning_ref/README.md @@ -1,3 +1,7 @@ +[![Build Status](https://travis-ci.org/Kimundi/owning-ref-rs.svg)](https://travis-ci.org/Kimundi/owning-ref-rs) +[![Crate](https://img.shields.io/crates/v/owning_ref.svg)](https://crates.io/crates/owning_ref) +[![Docs](https://docs.rs/owning_ref/badge.svg)](https://docs.rs/owning_ref) + owning-ref-rs ============== @@ -26,25 +30,18 @@ fn return_owned_and_referenced() -> OwningRef, [u8]> { } ``` -[![Travis-CI Status](https://travis-ci.org/Kimundi/owning-ref-rs.png?branch=master)](https://travis-ci.org/Kimundi/owning-ref-rs) +## Getting Started -# Getting Started - -[owning-ref-rs is available on crates.io](https://crates.io/crates/owning_ref). -It is recommended to look there for the newest released version, as well as links to the newest builds of the docs. - -At the point of the last update of this README, the latest published version could be used like this: - -Add the following dependency to your Cargo manifest... +To get started, add the following to `Cargo.toml`. ```toml -[dependencies] -owning_ref = "0.3" +owning_ref = "0.4.1" ``` ...and see the [docs](http://kimundi.github.io/owning-ref-rs/owning_ref/index.html) for how to use it. -# Example + +## Example ```rust extern crate owning_ref; diff --git a/third_party/rust/owning_ref/src/lib.rs b/third_party/rust/owning_ref/src/lib.rs index 7a291361e0ff..fa2f15a22056 100644 --- a/third_party/rust/owning_ref/src/lib.rs +++ b/third_party/rust/owning_ref/src/lib.rs @@ -281,7 +281,7 @@ pub trait Erased {} impl Erased for T {} /// Helper trait for erasing the concrete type of what an owner derferences to, -/// for example `Box -> Box`. This would be unneeded with +/// for example `Box -> Box`. This would be unneeded with /// higher kinded types support in the language. pub unsafe trait IntoErased<'a> { /// Owner with the dereference type substituted to `Erased`. @@ -362,6 +362,38 @@ impl OwningRef { } } + /// Converts `self` into a new owning reference that points at something reachable + /// from the previous one or from the owner itself. + /// + /// This can be a reference to a field of `U`, something reachable from a field of + /// `U` or from the owner `O`, or even something unrelated with a `'static` lifetime. + /// + /// # Example + /// ``` + /// extern crate owning_ref; + /// use owning_ref::OwningRef; + /// + /// fn main() { + /// let owning_ref = OwningRef::new(Box::new([1, 2, 3, 4])); + /// let owning_ref = owning_ref.map(|array| &array[2]); + /// assert_eq!(*owning_ref, 3); + /// + /// // create a owning reference that points at the + /// // second element of the array from the owning ref that was pointing to the third + /// let owning_ref = owning_ref.map_with_owner(|array, _prev| &array[1]); + /// assert_eq!(*owning_ref, 2); + /// } + /// ``` + pub fn map_with_owner(self, f: F) -> OwningRef + where O: StableAddress, + F: for<'a> FnOnce(&'a O, &'a T) -> &'a U + { + OwningRef { + reference: f(&self.owner, &self), + owner: self.owner, + } + } + /// Tries to convert `self` into a new owning reference that points /// at something reachable from the previous one. /// @@ -394,6 +426,39 @@ impl OwningRef { }) } + /// Tries to convert `self` into a new owning reference that points + /// at something reachable from the previous one. + /// + /// This can be a reference to a field of `U`, something reachable from a field of + /// `U`, or even something unrelated with a `'static` lifetime. + /// + /// # Example + /// ``` + /// extern crate owning_ref; + /// use owning_ref::OwningRef; + /// + /// fn main() { + /// let owning_ref = OwningRef::new(Box::new([1, 2, 3, 4])); + /// let owning_ref = owning_ref.map(|array| &array[2]); + /// + /// // create a owning reference that points at the + /// // second element of the array from the owning ref that was pointing to the third + /// let owning_ref = owning_ref.try_map_with_owner(|array, _prev| { + /// if array[1] == 2 { Ok(&array[1]) } else { Err(()) } + /// }); + /// assert_eq!(*owning_ref.unwrap(), 2); + /// } + /// ``` + pub fn try_map_with_owner(self, f: F) -> Result, E> + where O: StableAddress, + F: for<'a> FnOnce(&'a O, &'a T) -> Result<&'a U, E> + { + Ok(OwningRef { + reference: f(&self.owner, &self)?, + owner: self.owner, + }) + } + /// Converts `self` into a new owning reference with a different owner type. /// /// The new owner type needs to still contain the original owner in some way @@ -414,7 +479,7 @@ impl OwningRef { /// in an additional `Box`. /// /// This can be used to safely erase the owner of any `OwningRef` - /// to a `OwningRef, T>`. + /// to a `OwningRef, T>`. pub fn map_owner_box(self) -> OwningRef, T> { OwningRef { reference: self.reference, @@ -447,7 +512,7 @@ impl OwningRef { /// let owning_ref_b: OwningRef>, i32> /// = owning_ref_b.map(|a| &a[1].0); /// - /// let owning_refs: [OwningRef, i32>; 2] + /// let owning_refs: [OwningRef, i32>; 2] /// = [owning_ref_a.erase_owner(), owning_ref_b.erase_owner()]; /// /// assert_eq!(*owning_refs[0], 1); @@ -658,7 +723,7 @@ impl OwningRefMut { /// in an additional `Box`. /// /// This can be used to safely erase the owner of any `OwningRefMut` - /// to a `OwningRefMut, T>`. + /// to a `OwningRefMut, T>`. pub fn map_owner_box(self) -> OwningRefMut, T> { OwningRefMut { reference: self.reference, @@ -691,7 +756,7 @@ impl OwningRefMut { /// let owning_ref_mut_b: OwningRefMut>, i32> /// = owning_ref_mut_b.map_mut(|a| &mut a[1].0); /// - /// let owning_refs_mut: [OwningRefMut, i32>; 2] + /// let owning_refs_mut: [OwningRefMut, i32>; 2] /// = [owning_ref_mut_a.erase_owner(), owning_ref_mut_b.erase_owner()]; /// /// assert_eq!(*owning_refs_mut[0], 1); @@ -1015,9 +1080,9 @@ unsafe impl Send for OwningRefMut unsafe impl Sync for OwningRefMut where O: Sync, for<'a> (&'a mut T): Sync {} -impl Debug for Erased { +impl Debug for dyn Erased { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "",) + write!(f, "",) } } @@ -1135,33 +1200,33 @@ pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut, U>; pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRefMut, U>; unsafe impl<'a, T: 'a> IntoErased<'a> for Box { - type Erased = Box; + type Erased = Box; fn into_erased(self) -> Self::Erased { self } } unsafe impl<'a, T: 'a> IntoErased<'a> for Rc { - type Erased = Rc; + type Erased = Rc; fn into_erased(self) -> Self::Erased { self } } unsafe impl<'a, T: 'a> IntoErased<'a> for Arc { - type Erased = Arc; + type Erased = Arc; fn into_erased(self) -> Self::Erased { self } } /// Typedef of a owning reference that uses an erased `Box` as the owner. -pub type ErasedBoxRef = OwningRef, U>; +pub type ErasedBoxRef = OwningRef, U>; /// Typedef of a owning reference that uses an erased `Rc` as the owner. -pub type ErasedRcRef = OwningRef, U>; +pub type ErasedRcRef = OwningRef, U>; /// Typedef of a owning reference that uses an erased `Arc` as the owner. -pub type ErasedArcRef = OwningRef, U>; +pub type ErasedArcRef = OwningRef, U>; /// Typedef of a mutable owning reference that uses an erased `Box` as the owner. -pub type ErasedBoxRefMut = OwningRefMut, U>; +pub type ErasedBoxRefMut = OwningRefMut, U>; #[cfg(test)] mod tests { @@ -1284,7 +1349,7 @@ mod tests { let o: BoxRef<&[i32; 2]> = Box::new(bar).into(); let o: BoxRef<&[i32; 2], i32> = o.map(borrow); - let o: BoxRef = o.erase_owner(); + let o: BoxRef = o.erase_owner(); assert_eq!(*o, 413); } @@ -1405,8 +1470,8 @@ mod tests { let c: OwningRef>, [u8]> = unsafe {a.map_owner(Rc::new)}; let d: OwningRef>, [u8]> = unsafe {b.map_owner(Rc::new)}; - let e: OwningRef, [u8]> = c.erase_owner(); - let f: OwningRef, [u8]> = d.erase_owner(); + let e: OwningRef, [u8]> = c.erase_owner(); + let f: OwningRef, [u8]> = d.erase_owner(); let _g = e.clone(); let _h = f.clone(); @@ -1422,8 +1487,8 @@ mod tests { let c: OwningRef>, [u8]> = a.map_owner_box(); let d: OwningRef>, [u8]> = b.map_owner_box(); - let _e: OwningRef, [u8]> = c.erase_owner(); - let _f: OwningRef, [u8]> = d.erase_owner(); + let _e: OwningRef, [u8]> = c.erase_owner(); + let _f: OwningRef, [u8]> = d.erase_owner(); } #[test] @@ -1431,19 +1496,52 @@ mod tests { use std::any::Any; let x = Box::new(123_i32); - let y: Box = x; + let y: Box = x; - OwningRef::new(y).try_map(|x| x.downcast_ref::().ok_or(())).is_ok(); + OwningRef::new(y).try_map(|x| x.downcast_ref::().ok_or(())).unwrap(); } #[test] fn try_map2() { use std::any::Any; - let x = Box::new(123_i32); - let y: Box = x; + let x = Box::new(123_u32); + let y: Box = x; - OwningRef::new(y).try_map(|x| x.downcast_ref::().ok_or(())).is_err(); + OwningRef::new(y).try_map(|x| x.downcast_ref::().ok_or(())).unwrap_err(); + } + + #[test] + fn map_with_owner() { + let owning_ref: BoxRef = Box::new(example()).into(); + let owning_ref = owning_ref.map(|owner| &owner.1); + + owning_ref.map_with_owner(|owner, ref_field| { + assert_eq!(owner.1, *ref_field); + ref_field + }); + } + + #[test] + fn try_map_with_owner_ok() { + let owning_ref: BoxRef = Box::new(example()).into(); + let owning_ref = owning_ref.map(|owner| &owner.1); + + owning_ref.try_map_with_owner(|owner, ref_field| { + assert_eq!(owner.1, *ref_field); + Ok(ref_field) as Result<_, ()> + }).unwrap(); + } + + #[test] + fn try_map_with_owner_err() { + let owning_ref: BoxRef = Box::new(example()).into(); + let owning_ref = owning_ref.map(|owner| &owner.1); + + owning_ref.try_map_with_owner(|owner, ref_field| { + assert_eq!(owner.1, *ref_field); + Err(()) as Result<&(), _> + }).unwrap_err(); } } @@ -1722,7 +1820,7 @@ mod tests { let o: BoxRefMut<&mut [i32; 2]> = Box::new(bar).into(); let o: BoxRefMut<&mut [i32; 2], i32> = o.map_mut(borrow); - let o: BoxRefMut = o.erase_owner(); + let o: BoxRefMut = o.erase_owner(); assert_eq!(*o, 413); } @@ -1824,8 +1922,8 @@ mod tests { let c: OwningRefMut>, [u8]> = unsafe {a.map_owner(Box::new)}; let d: OwningRefMut>, [u8]> = unsafe {b.map_owner(Box::new)}; - let _e: OwningRefMut, [u8]> = c.erase_owner(); - let _f: OwningRefMut, [u8]> = d.erase_owner(); + let _e: OwningRefMut, [u8]> = c.erase_owner(); + let _f: OwningRefMut, [u8]> = d.erase_owner(); } #[test] @@ -1838,8 +1936,8 @@ mod tests { let c: OwningRefMut>, [u8]> = a.map_owner_box(); let d: OwningRefMut>, [u8]> = b.map_owner_box(); - let _e: OwningRefMut, [u8]> = c.erase_owner(); - let _f: OwningRefMut, [u8]> = d.erase_owner(); + let _e: OwningRefMut, [u8]> = c.erase_owner(); + let _f: OwningRefMut, [u8]> = d.erase_owner(); } #[test] @@ -1847,19 +1945,19 @@ mod tests { use std::any::Any; let x = Box::new(123_i32); - let y: Box = x; + let y: Box = x; - OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::().ok_or(())).is_ok(); + OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::().ok_or(())).unwrap(); } #[test] fn try_map2() { use std::any::Any; - let x = Box::new(123_i32); - let y: Box = x; + let x = Box::new(123_u32); + let y: Box = x; - OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::().ok_or(())).is_err(); + OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::().ok_or(())).unwrap_err(); } #[test] @@ -1867,19 +1965,19 @@ mod tests { use std::any::Any; let x = Box::new(123_i32); - let y: Box = x; + let y: Box = x; - OwningRefMut::new(y).try_map(|x| x.downcast_ref::().ok_or(())).is_ok(); + OwningRefMut::new(y).try_map(|x| x.downcast_ref::().ok_or(())).unwrap(); } #[test] fn try_map4() { use std::any::Any; - let x = Box::new(123_i32); - let y: Box = x; + let x = Box::new(123_u32); + let y: Box = x; - OwningRefMut::new(y).try_map(|x| x.downcast_ref::().ok_or(())).is_err(); + OwningRefMut::new(y).try_map(|x| x.downcast_ref::().ok_or(())).unwrap_err(); } #[test]