Bug 1518045 - Update owning_ref to 0.4.

This cherry-picks https://github.com/servo/servo/pull/22534, plus the relevant
re-vendoring.
This commit is contained in:
Bastien Orivel 2018-12-22 21:02:42 +01:00 committed by Emilio Cobos Álvarez
parent b1ddabc921
commit 24db8c61b4
14 changed files with 543 additions and 62 deletions

14
Cargo.lock generated
View file

@ -1435,10 +1435,10 @@ dependencies = [
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.1.3" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"scopeguard 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "scopeguard 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1830,7 +1830,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "owning_ref" name = "owning_ref"
version = "0.3.3" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1841,7 +1841,7 @@ name = "parking_lot"
version = "0.6.3" version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -2440,7 +2440,7 @@ dependencies = [
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3308,7 +3308,7 @@ dependencies = [
"checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e" "checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e"
"checksum lmdb-rkv 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "07d8d72d709440ac7a19b71e04fb66684ec7547b3831817872454b01fdaefe23" "checksum lmdb-rkv 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "07d8d72d709440ac7a19b71e04fb66684ec7547b3831817872454b01fdaefe23"
"checksum lmdb-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5b392838cfe8858e86fac37cf97a0e8c55cc60ba0a18365cadc33092f128ce9" "checksum lmdb-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5b392838cfe8858e86fac37cf97a0e8c55cc60ba0a18365cadc33092f128ce9"
"checksum lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
"checksum log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fcce5fa49cc693c312001daf1d13411c4a5283796bac1084299ea3e567113f" "checksum log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fcce5fa49cc693c312001daf1d13411c4a5283796bac1084299ea3e567113f"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376" "checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
@ -3339,7 +3339,7 @@ dependencies = [
"checksum opaque-debug 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "51ecbcb821e1bd256d456fe858aaa7f380b63863eab2eb86eee1bd9f33dd6682" "checksum opaque-debug 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "51ecbcb821e1bd256d456fe858aaa7f380b63863eab2eb86eee1bd9f33dd6682"
"checksum ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f0015e9e8e28ee20c581cfbfe47c650cedeb9ed0721090e0b7ebb10b9cdbcc2" "checksum ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f0015e9e8e28ee20c581cfbfe47c650cedeb9ed0721090e0b7ebb10b9cdbcc2"
"checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"
"checksum parking_lot 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "69376b761943787ebd5cc85a5bc95958651a22609c5c1c2b65de21786baec72b" "checksum parking_lot 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "69376b761943787ebd5cc85a5bc95958651a22609c5c1c2b65de21786baec72b"
"checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa" "checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa"
"checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" "checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"

View file

@ -52,7 +52,7 @@ num-integer = "0.1"
num-traits = "0.2" num-traits = "0.2"
num-derive = "0.2" num-derive = "0.2"
ordered-float = "1.0" ordered-float = "1.0"
owning_ref = "0.3.3" owning_ref = "0.4"
parking_lot = "0.6" parking_lot = "0.6"
precomputed-hash = "0.1.1" precomputed-hash = "0.1.1"
rayon = "1" rayon = "1"

View file

@ -1 +1 @@
{"files":{"Cargo.toml":"9a369b3ff1ef121205879d10ba966bbccb81d6461843a75460f4cfcb82c3c849","src/lib.rs":"62142edfbe7e6cc04473aef25ab42553f02e4455ce567cb4303f0bbfee94d87d","src/mutex.rs":"d7ea4e06ae1a2d470e23bdfe73f52272f7c8b4231fe62fd9846e2a3773dcface","src/remutex.rs":"2fc11fec14af2886e507104e501caf6fddcf199ee7db38eabcd35ab9cf430d89","src/rwlock.rs":"45b940a9d046c7ee0c295237abac45f694c32a247724d93e3879a79f6e60b399"},"package":"949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54"} {"files":{"Cargo.toml":"ab2a7a96105e15de46900fb0da37edbab44e5513a9818672153dae44ed318f7e","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"c9a75f18b9ab2927829a208fc6aa2cf4e63b8420887ba29cdb265d6619ae82d5","src/lib.rs":"4a16128f58e3380b22b26b137ee1096732995b7e401f3d227dd7b0738b6bd604","src/mutex.rs":"fee397f72325621812c5f78c7a6b9369ea7ec14e71bb0049678a50349519c0c7","src/remutex.rs":"ed76d7b93a56b6248d79676de2aaa66b607b64f1b773c9dd7326b8324e2bc71a","src/rwlock.rs":"5ab1aab614358cfdaf23e8ff8a0ac5e0c7656b777f385aca2e5422f0aa8f0985"},"package":"62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"}

View file

@ -12,7 +12,7 @@
[package] [package]
name = "lock_api" name = "lock_api"
version = "0.1.3" version = "0.1.5"
authors = ["Amanieu d'Antras <amanieu@gmail.com>"] authors = ["Amanieu d'Antras <amanieu@gmail.com>"]
description = "Wrappers to create fully-featured Mutex and RwLock types. Compatible with no_std." description = "Wrappers to create fully-featured Mutex and RwLock types. Compatible with no_std."
keywords = ["mutex", "rwlock", "lock", "no_std"] keywords = ["mutex", "rwlock", "lock", "no_std"]
@ -20,7 +20,7 @@ categories = ["concurrency", "no-std"]
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
repository = "https://github.com/Amanieu/parking_lot" repository = "https://github.com/Amanieu/parking_lot"
[dependencies.owning_ref] [dependencies.owning_ref]
version = "0.3" version = "0.4"
optional = true optional = true
[dependencies.scopeguard] [dependencies.scopeguard]

201
third_party/rust/lock_api/LICENSE-APACHE vendored Normal file
View file

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

25
third_party/rust/lock_api/LICENSE-MIT vendored Normal file
View file

@ -0,0 +1,25 @@
Copyright (c) 2016 The Rust Project Developers
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View file

@ -11,7 +11,7 @@
//! implementing custom lock types, it also allows users to write code which is //! implementing custom lock types, it also allows users to write code which is
//! generic with regards to different lock implementations. //! generic with regards to different lock implementations.
//! //!
//! Basic usage of this crate is very straightfoward: //! Basic usage of this crate is very straightforward:
//! //!
//! 1. Create a raw lock type. This should only contain the lock state, not any //! 1. Create a raw lock type. This should only contain the lock state, not any
//! data protected by the lock. //! data protected by the lock.
@ -27,7 +27,7 @@
//! # Example //! # Example
//! //!
//! ``` //! ```
//! use lock_api::{RawMutex, Mutex}; //! use lock_api::{RawMutex, Mutex, GuardSend};
//! use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT}; //! use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
//! //!
//! // 1. Define our raw lock type //! // 1. Define our raw lock type
@ -37,6 +37,9 @@
//! unsafe impl RawMutex for RawSpinlock { //! unsafe impl RawMutex for RawSpinlock {
//! const INIT: RawSpinlock = RawSpinlock(ATOMIC_BOOL_INIT); //! const INIT: RawSpinlock = RawSpinlock(ATOMIC_BOOL_INIT);
//! //!
//! // A spinlock guard can be sent to another thread and unlocked there
//! type GuardMarker = GuardSend;
//!
//! fn lock(&self) { //! fn lock(&self) {
//! // Note: This isn't the best way of implementing a spinlock, but it //! // Note: This isn't the best way of implementing a spinlock, but it
//! // suffices for the sake of this example. //! // suffices for the sake of this example.

View file

@ -318,6 +318,33 @@ impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MutexGuard<'a, R, T> {
} }
} }
/// Attempts to make a new `MappedMutexGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `MutexGuard` passed
/// in already locked the mutex.
///
/// This is an associated function that needs to be
/// used as `MutexGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedMutexGuard<'a, R, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = &s.mutex.raw;
let data = match f(unsafe { &mut *s.mutex.data.get() }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedMutexGuard {
raw,
data,
marker: PhantomData,
})
}
/// Temporarily unlocks the mutex to execute the given function. /// Temporarily unlocks the mutex to execute the given function.
/// ///
/// This is safe because `&mut` guarantees that there exist no other /// This is safe because `&mut` guarantees that there exist no other
@ -428,11 +455,11 @@ unsafe impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Send for MappedMutexGuard<'a,
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T> { impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T> {
/// Makes a new `MappedMutexGuard` for a component of the locked data. /// Makes a new `MappedMutexGuard` for a component of the locked data.
/// ///
/// This operation cannot fail as the `MutexGuard` passed /// This operation cannot fail as the `MappedMutexGuard` passed
/// in already locked the mutex. /// in already locked the mutex.
/// ///
/// This is an associated function that needs to be /// This is an associated function that needs to be
/// used as `MutexGuard::map(...)`. A method would interfere with methods of /// used as `MappedMutexGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data. /// the same name on the contents of the locked data.
#[inline] #[inline]
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedMutexGuard<'a, R, U> pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedMutexGuard<'a, R, U>
@ -448,6 +475,33 @@ impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T> {
marker: PhantomData, marker: PhantomData,
} }
} }
/// Attempts to make a new `MappedMutexGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `MappedMutexGuard` passed
/// in already locked the mutex.
///
/// This is an associated function that needs to be
/// used as `MappedMutexGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedMutexGuard<'a, R, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = s.raw;
let data = match f(unsafe { &mut *s.data }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedMutexGuard {
raw,
data,
marker: PhantomData,
})
}
} }
impl<'a, R: RawMutexFair + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T> { impl<'a, R: RawMutexFair + 'a, T: ?Sized + 'a> MappedMutexGuard<'a, R, T> {

View file

@ -268,7 +268,7 @@ impl<R: RawMutex, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
} }
impl<R: RawMutexFair, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> { impl<R: RawMutexFair, G: GetThreadId, T: ?Sized> ReentrantMutex<R, G, T> {
/// Forcibly unlocks the mutex using a fair unlock procotol. /// Forcibly unlocks the mutex using a fair unlock protocol.
/// ///
/// This is useful when combined with `mem::forget` to hold a lock without /// This is useful when combined with `mem::forget` to hold a lock without
/// the need to maintain a `ReentrantMutexGuard` object alive, for example when /// the need to maintain a `ReentrantMutexGuard` object alive, for example when
@ -385,6 +385,33 @@ impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> ReentrantMutexGu
} }
} }
/// Attempts to make a new `MappedReentrantMutexGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `ReentrantMutexGuard` passed
/// in already locked the mutex.
///
/// This is an associated function that needs to be
/// used as `ReentrantMutexGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedReentrantMutexGuard<'a, R, G, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = &s.remutex.raw;
let data = match f(unsafe { &mut *s.remutex.data.get() }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedReentrantMutexGuard {
raw,
data,
marker: PhantomData,
})
}
/// Temporarily unlocks the mutex to execute the given function. /// Temporarily unlocks the mutex to execute the given function.
/// ///
/// This is safe because `&mut` guarantees that there exist no other /// This is safe because `&mut` guarantees that there exist no other
@ -495,11 +522,11 @@ impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a>
{ {
/// Makes a new `MappedReentrantMutexGuard` for a component of the locked data. /// Makes a new `MappedReentrantMutexGuard` for a component of the locked data.
/// ///
/// This operation cannot fail as the `ReentrantMutexGuard` passed /// This operation cannot fail as the `MappedReentrantMutexGuard` passed
/// in already locked the mutex. /// in already locked the mutex.
/// ///
/// This is an associated function that needs to be /// This is an associated function that needs to be
/// used as `ReentrantMutexGuard::map(...)`. A method would interfere with methods of /// used as `MappedReentrantMutexGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data. /// the same name on the contents of the locked data.
#[inline] #[inline]
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedReentrantMutexGuard<'a, R, G, U> pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedReentrantMutexGuard<'a, R, G, U>
@ -515,6 +542,33 @@ impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a>
marker: PhantomData, marker: PhantomData,
} }
} }
/// Attempts to make a new `MappedReentrantMutexGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `MappedReentrantMutexGuard` passed
/// in already locked the mutex.
///
/// This is an associated function that needs to be
/// used as `MappedReentrantMutexGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedReentrantMutexGuard<'a, R, G, U>, Self>
where
F: FnOnce(&T) -> Option<&U>,
{
let raw = s.raw;
let data = match f(unsafe { &*s.data }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedReentrantMutexGuard {
raw,
data,
marker: PhantomData,
})
}
} }
impl<'a, R: RawMutexFair + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> impl<'a, R: RawMutexFair + 'a, G: GetThreadId + 'a, T: ?Sized + 'a>

View file

@ -714,6 +714,33 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockReadGuard<'a, R, T> {
} }
} }
/// Attempts to make a new `MappedRwLockReadGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `RwLockReadGuard` passed
/// in already locked the data.
///
/// This is an associated function that needs to be
/// used as `RwLockReadGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockReadGuard<'a, R, U>, Self>
where
F: FnOnce(&T) -> Option<&U>,
{
let raw = &s.rwlock.raw;
let data = match f(unsafe { &*s.rwlock.data.get() }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
})
}
/// Temporarily unlocks the `RwLock` to execute the given function. /// Temporarily unlocks the `RwLock` to execute the given function.
/// ///
/// The `RwLock` is unlocked a fair unlock protocol. /// The `RwLock` is unlocked a fair unlock protocol.
@ -820,13 +847,13 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockWriteGuard<'a, R, T> {
/// used as `RwLockWriteGuard::map(...)`. A method would interfere with methods of /// used as `RwLockWriteGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data. /// the same name on the contents of the locked data.
#[inline] #[inline]
pub fn map<U: ?Sized, F>(orig: Self, f: F) -> MappedRwLockWriteGuard<'a, R, U> pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockWriteGuard<'a, R, U>
where where
F: FnOnce(&mut T) -> &mut U, F: FnOnce(&mut T) -> &mut U,
{ {
let raw = &orig.rwlock.raw; let raw = &s.rwlock.raw;
let data = f(unsafe { &mut *orig.rwlock.data.get() }); let data = f(unsafe { &mut *s.rwlock.data.get() });
mem::forget(orig); mem::forget(s);
MappedRwLockWriteGuard { MappedRwLockWriteGuard {
raw, raw,
data, data,
@ -834,6 +861,33 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> RwLockWriteGuard<'a, R, T> {
} }
} }
/// Attempts to make a new `MappedRwLockWriteGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `RwLockWriteGuard` passed
/// in already locked the data.
///
/// This is an associated function that needs to be
/// used as `RwLockWriteGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockWriteGuard<'a, R, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = &s.rwlock.raw;
let data = match f(unsafe { &mut *s.rwlock.data.get() }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockWriteGuard {
raw,
data,
marker: PhantomData,
})
}
/// Temporarily unlocks the `RwLock` to execute the given function. /// Temporarily unlocks the `RwLock` to execute the given function.
/// ///
/// This is safe because `&mut` guarantees that there exist no other /// This is safe because `&mut` guarantees that there exist no other
@ -989,7 +1043,7 @@ impl<'a, R: RawRwLockUpgrade + 'a, T: ?Sized + 'a> RwLockUpgradableReadGuard<'a,
} }
/// Atomically upgrades an upgradable read lock lock into a exclusive write lock, /// Atomically upgrades an upgradable read lock lock into a exclusive write lock,
/// blocking the current thread until it can be aquired. /// blocking the current thread until it can be acquired.
pub fn upgrade(s: Self) -> RwLockWriteGuard<'a, R, T> { pub fn upgrade(s: Self) -> RwLockWriteGuard<'a, R, T> {
s.rwlock.raw.upgrade(); s.rwlock.raw.upgrade();
let rwlock = s.rwlock; let rwlock = s.rwlock;
@ -1189,6 +1243,33 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> MappedRwLockReadGuard<'a, R, T> {
marker: PhantomData, marker: PhantomData,
} }
} }
/// Attempts to make a new `MappedRwLockReadGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `MappedRwLockReadGuard` passed
/// in already locked the data.
///
/// This is an associated function that needs to be
/// used as `MappedRwLockReadGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockReadGuard<'a, R, U>, Self>
where
F: FnOnce(&T) -> Option<&U>,
{
let raw = s.raw;
let data = match f(unsafe { &*s.data }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
})
}
} }
impl<'a, R: RawRwLockFair + 'a, T: ?Sized + 'a> MappedRwLockReadGuard<'a, R, T> { impl<'a, R: RawRwLockFair + 'a, T: ?Sized + 'a> MappedRwLockReadGuard<'a, R, T> {
@ -1262,19 +1343,46 @@ impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> MappedRwLockWriteGuard<'a, R, T> {
/// used as `MappedRwLockWriteGuard::map(...)`. A method would interfere with methods of /// used as `MappedRwLockWriteGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data. /// the same name on the contents of the locked data.
#[inline] #[inline]
pub fn map<U: ?Sized, F>(orig: Self, f: F) -> MappedRwLockWriteGuard<'a, R, U> pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockWriteGuard<'a, R, U>
where where
F: FnOnce(&mut T) -> &mut U, F: FnOnce(&mut T) -> &mut U,
{ {
let raw = orig.raw; let raw = s.raw;
let data = f(unsafe { &mut *orig.data }); let data = f(unsafe { &mut *s.data });
mem::forget(orig); mem::forget(s);
MappedRwLockWriteGuard { MappedRwLockWriteGuard {
raw, raw,
data, data,
marker: PhantomData, marker: PhantomData,
} }
} }
/// Attempts to make a new `MappedRwLockWriteGuard` for a component of the
/// locked data. The original guard is return if the closure returns `None`.
///
/// This operation cannot fail as the `MappedRwLockWriteGuard` passed
/// in already locked the data.
///
/// This is an associated function that needs to be
/// used as `MappedRwLockWriteGuard::map(...)`. A method would interfere with methods of
/// the same name on the contents of the locked data.
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockWriteGuard<'a, R, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = s.raw;
let data = match f(unsafe { &mut *s.data }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockWriteGuard {
raw,
data,
marker: PhantomData,
})
}
} }
impl<'a, R: RawRwLockDowngrade + 'a, T: ?Sized + 'a> MappedRwLockWriteGuard<'a, R, T> { impl<'a, R: RawRwLockDowngrade + 'a, T: ?Sized + 'a> MappedRwLockWriteGuard<'a, R, T> {

View file

@ -1 +1 @@
{"files":{"Cargo.toml":"d6033830eecd6112eb61e775f848ab6c7ee76822ee1d7f8786b34e6e71f45b7b","LICENSE":"90bc15ed094593083fd129fdd1a03607be80fe8839c5564616a5961ab7f7a194","README.md":"e18ff10a148a8316e89bbe9f45cb57657170abe1a19154f8a5c968d529fe895e","src/lib.rs":"5d8857eee0de9863c3b6fce98732427cc1868a5ec3f0e3ce652ec011391aa842"},"package":"cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"} {"files":{"Cargo.toml":"9cc8936f2504375b24b88060b766a908e69690246cd063138b3a9b39287640d7","LICENSE":"90bc15ed094593083fd129fdd1a03607be80fe8839c5564616a5961ab7f7a194","README.md":"a479bb465878f3cbb8d53d2f42a26ac309e121d7c635956b7547a6bdfedce5d4","src/lib.rs":"e1e523638bea245fcf00b6643c61f856f2404f4d5d9ed0c230a6c4d2d8550b1c"},"package":"49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"}

View file

@ -1,15 +1,24 @@
# 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package] [package]
name = "owning_ref" name = "owning_ref"
version = "0.3.3" version = "0.4.0"
authors = ["Marvin Löbel <loebel.marvin@gmail.com>"] authors = ["Marvin Löbel <loebel.marvin@gmail.com>"]
license = "MIT"
description = "A library for creating references that carry their owner with them." description = "A library for creating references that carry their owner with them."
readme = "README.md"
documentation = "http://kimundi.github.io/owning-ref-rs/owning_ref/index.html" documentation = "http://kimundi.github.io/owning-ref-rs/owning_ref/index.html"
readme = "README.md"
repository = "https://github.com/Kimundi/owning-ref-rs"
keywords = ["reference", "sibling", "field", "owning"] keywords = ["reference", "sibling", "field", "owning"]
license = "MIT"
[dependencies] repository = "https://github.com/Kimundi/owning-ref-rs"
stable_deref_trait = "1.0.0" [dependencies.stable_deref_trait]
version = "1.0.0"

View file

@ -4,7 +4,7 @@ owning-ref-rs
A library for creating references that carry their owner with them. A library for creating references that carry their owner with them.
This can sometimes be useful because Rust borrowing rules normally prevent This can sometimes be useful because Rust borrowing rules normally prevent
moving a type that has been moved from. For example, this kind of code gets rejected: moving a type that has been borrowed from. For example, this kind of code gets rejected:
```rust ```rust
fn return_owned_and_referenced<'a>() -> (Vec<u8>, &'a [u8]) { fn return_owned_and_referenced<'a>() -> (Vec<u8>, &'a [u8]) {

View file

@ -465,15 +465,13 @@ impl<O, T: ?Sized> OwningRef<O, T> {
// TODO: wrap_owner // TODO: wrap_owner
// FIXME: Naming convention? /// A reference to the underlying owner.
/// A getter for the underlying owner. pub fn as_owner(&self) -> &O {
pub fn owner(&self) -> &O {
&self.owner &self.owner
} }
// FIXME: Naming convention?
/// Discards the reference and retrieves the owner. /// Discards the reference and retrieves the owner.
pub fn into_inner(self) -> O { pub fn into_owner(self) -> O {
self.owner self.owner
} }
} }
@ -711,15 +709,18 @@ impl<O, T: ?Sized> OwningRefMut<O, T> {
// TODO: wrap_owner // TODO: wrap_owner
// FIXME: Naming convention? /// A reference to the underlying owner.
/// A getter for the underlying owner. pub fn as_owner(&self) -> &O {
pub fn owner(&self) -> &O {
&self.owner &self.owner
} }
// FIXME: Naming convention? /// A mutable reference to the underlying owner.
pub fn as_owner_mut(&mut self) -> &mut O {
&mut self.owner
}
/// Discards the reference and retrieves the owner. /// Discards the reference and retrieves the owner.
pub fn into_inner(self) -> O { pub fn into_owner(self) -> O {
self.owner self.owner
} }
} }
@ -855,6 +856,16 @@ impl<O, H> OwningHandle<O, H>
_owner: o, _owner: o,
}) })
} }
/// A getter for the underlying owner.
pub fn as_owner(&self) -> &O {
&self._owner
}
/// Discards the dependent object and returns the owner.
pub fn into_owner(self) -> O {
self._owner
}
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@ -898,6 +909,8 @@ impl<O, T: ?Sized> DerefMut for OwningRefMut<O, T> {
unsafe impl<O, T: ?Sized> StableAddress for OwningRef<O, T> {} unsafe impl<O, T: ?Sized> StableAddress for OwningRef<O, T> {}
unsafe impl<O, T: ?Sized> StableAddress for OwningRefMut<O, T> {}
impl<O, T: ?Sized> AsRef<T> for OwningRef<O, T> { impl<O, T: ?Sized> AsRef<T> for OwningRef<O, T> {
fn as_ref(&self) -> &T { fn as_ref(&self) -> &T {
&*self &*self
@ -952,7 +965,7 @@ impl<O, T: ?Sized> From<OwningRefMut<O, T>> for OwningRef<O, T>
} }
} }
// ^ FIXME: Is a Into impl for calling into_inner() possible as well? // ^ FIXME: Is a Into impl for calling into_owner() possible as well?
impl<O, T: ?Sized> Debug for OwningRef<O, T> impl<O, T: ?Sized> Debug for OwningRef<O, T>
where O: Debug, where O: Debug,
@ -961,7 +974,7 @@ impl<O, T: ?Sized> Debug for OwningRef<O, T>
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, write!(f,
"OwningRef {{ owner: {:?}, reference: {:?} }}", "OwningRef {{ owner: {:?}, reference: {:?} }}",
self.owner(), self.as_owner(),
&**self) &**self)
} }
} }
@ -973,7 +986,7 @@ impl<O, T: ?Sized> Debug for OwningRefMut<O, T>
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, write!(f,
"OwningRefMut {{ owner: {:?}, reference: {:?} }}", "OwningRefMut {{ owner: {:?}, reference: {:?} }}",
self.owner(), self.as_owner(),
&**self) &**self)
} }
} }
@ -1119,7 +1132,7 @@ pub type RefMutRefMut<'a, T, U = T> = OwningRefMut<RefMut<'a, T>, U>;
/// Typedef of a mutable owning reference that uses a `MutexGuard` as the owner. /// Typedef of a mutable owning reference that uses a `MutexGuard` as the owner.
pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut<MutexGuard<'a, T>, U>; pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut<MutexGuard<'a, T>, U>;
/// Typedef of a mutable owning reference that uses a `RwLockWriteGuard` as the owner. /// Typedef of a mutable owning reference that uses a `RwLockWriteGuard` as the owner.
pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRef<RwLockWriteGuard<'a, T>, U>; pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRefMut<RwLockWriteGuard<'a, T>, U>;
unsafe impl<'a, T: 'a> IntoErased<'a> for Box<T> { unsafe impl<'a, T: 'a> IntoErased<'a> for Box<T> {
type Erased = Box<Erased + 'a>; type Erased = Box<Erased + 'a>;
@ -1221,19 +1234,19 @@ mod tests {
} }
#[test] #[test]
fn owner() { fn as_owner() {
let or: BoxRef<String> = Box::new(example().1).into(); let or: BoxRef<String> = Box::new(example().1).into();
let or = or.map(|x| &x[..5]); let or = or.map(|x| &x[..5]);
assert_eq!(&*or, "hello"); assert_eq!(&*or, "hello");
assert_eq!(&**or.owner(), "hello world"); assert_eq!(&**or.as_owner(), "hello world");
} }
#[test] #[test]
fn into_inner() { fn into_owner() {
let or: BoxRef<String> = Box::new(example().1).into(); let or: BoxRef<String> = Box::new(example().1).into();
let or = or.map(|x| &x[..5]); let or = or.map(|x| &x[..5]);
assert_eq!(&*or, "hello"); assert_eq!(&*or, "hello");
let s = *or.into_inner(); let s = *or.into_owner();
assert_eq!(&s, "hello world"); assert_eq!(&s, "hello world");
} }
@ -1262,8 +1275,15 @@ mod tests {
let foo = [413, 612]; let foo = [413, 612];
let bar = &foo; let bar = &foo;
// FIXME: lifetime inference fails us, and we can't easily define a lifetime for a closure
// (see https://github.com/rust-lang/rust/issues/22340)
// So we use a function to identify the lifetimes instead.
fn borrow<'a>(a: &'a &[i32; 2]) -> &'a i32 {
&a[0]
}
let o: BoxRef<&[i32; 2]> = Box::new(bar).into(); let o: BoxRef<&[i32; 2]> = Box::new(bar).into();
let o: BoxRef<&[i32; 2], i32> = o.map(|a: &&[i32; 2]| &a[0]); let o: BoxRef<&[i32; 2], i32> = o.map(borrow);
let o: BoxRef<Erased, i32> = o.erase_owner(); let o: BoxRef<Erased, i32> = o.erase_owner();
assert_eq!(*o, 413); assert_eq!(*o, 413);
@ -1651,19 +1671,19 @@ mod tests {
} }
#[test] #[test]
fn owner() { fn as_owner() {
let or: BoxRefMut<String> = Box::new(example().1).into(); let or: BoxRefMut<String> = Box::new(example().1).into();
let or = or.map_mut(|x| &mut x[..5]); let or = or.map_mut(|x| &mut x[..5]);
assert_eq!(&*or, "hello"); assert_eq!(&*or, "hello");
assert_eq!(&**or.owner(), "hello world"); assert_eq!(&**or.as_owner(), "hello world");
} }
#[test] #[test]
fn into_inner() { fn into_owner() {
let or: BoxRefMut<String> = Box::new(example().1).into(); let or: BoxRefMut<String> = Box::new(example().1).into();
let or = or.map_mut(|x| &mut x[..5]); let or = or.map_mut(|x| &mut x[..5]);
assert_eq!(&*or, "hello"); assert_eq!(&*or, "hello");
let s = *or.into_inner(); let s = *or.into_owner();
assert_eq!(&s, "hello world"); assert_eq!(&s, "hello world");
} }
@ -1693,8 +1713,15 @@ mod tests {
let mut foo = [413, 612]; let mut foo = [413, 612];
let bar = &mut foo; let bar = &mut foo;
// FIXME: lifetime inference fails us, and we can't easily define a lifetime for a closure
// (see https://github.com/rust-lang/rust/issues/22340)
// So we use a function to identify the lifetimes instead.
fn borrow<'a>(a: &'a mut &mut [i32; 2]) -> &'a mut i32 {
&mut a[0]
}
let o: BoxRefMut<&mut [i32; 2]> = Box::new(bar).into(); let o: BoxRefMut<&mut [i32; 2]> = Box::new(bar).into();
let o: BoxRefMut<&mut [i32; 2], i32> = o.map_mut(|a: &mut &mut [i32; 2]| &mut a[0]); let o: BoxRefMut<&mut [i32; 2], i32> = o.map_mut(borrow);
let o: BoxRefMut<Erased, i32> = o.erase_owner(); let o: BoxRefMut<Erased, i32> = o.erase_owner();
assert_eq!(*o, 413); assert_eq!(*o, 413);