зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1785002 - Update either to 1.7.0. r=emilio,supply-chain-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D154723
This commit is contained in:
Родитель
58b043844e
Коммит
9fd2e22cd6
|
@ -1428,9 +1428,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
version = "1.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_c"
|
||||
|
|
|
@ -173,6 +173,11 @@ criteria = "safe-to-deploy"
|
|||
delta = "0.4.0 -> 0.5.0"
|
||||
notes = "The repository for this crate belongs in the Mozilla org."
|
||||
|
||||
[[audits.either]]
|
||||
who = "Mike Hommey <mh+mozilla@glandium.org>"
|
||||
criteria = "safe-to-deploy"
|
||||
delta = "1.6.1 -> 1.7.0"
|
||||
|
||||
[[audits.extend]]
|
||||
who = "Ben Dean-Kawamura <bdk@mozilla.com>"
|
||||
criteria = "safe-to-deploy"
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"files":{"Cargo.toml":"d4e9a8625c1740b1a8973a2e58c04ec7007c92b6b1f8be7f7dfe106f0929ffeb","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"7576269ea71f767b99297934c0b2367532690f8c4badc695edf8e04ab6a1e545","README-crates.io.md":"b775991a01ab4a0a8de6169f597775319d9ce8178f5c74ccdc634f13a286b20c","README.rst":"ce419aa43bbed11a3e25e89720227f4d8653d346cefac9dee6364e0d933f7c3f","src/lib.rs":"72193806d28cff7d994453c81c50987004168407cc4793991e2d36fef285590b","src/serde_untagged.rs":"da462e39a93abd3c9537d124354e3066a74073a275893b78a0ef5bd203bf84c2","src/serde_untagged_optional.rs":"b654b2a5c6127ca295079369ee0b7bbbb2d92699cdd3672d4b932ea292057287"},"package":"e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"}
|
||||
{"files":{"Cargo.toml":"77c37f327af7a6146ba111c1c0a3e861bd444f31cfe44c3645c8e090502f66b4","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"7576269ea71f767b99297934c0b2367532690f8c4badc695edf8e04ab6a1e545","README-crates.io.md":"b775991a01ab4a0a8de6169f597775319d9ce8178f5c74ccdc634f13a286b20c","README.rst":"f948f4333e9d66477128bcaf69a449a139bf00633eb57491fa1b379a3ce30b74","src/lib.rs":"e0f7c05e75af07765c2298a1791c65ea80f3c099491ed95c2e6c771041aa9931","src/serde_untagged.rs":"e826ee0ab31616e49c3e3f3711c8441001ee424b3e7a8c4c466cfcc4f8a7701a","src/serde_untagged_optional.rs":"86265f09d0795428bb2ce013b070d1badf1e2210217844a9ff3f04b2795868ab"},"package":"3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be"}
|
|
@ -3,34 +3,46 @@
|
|||
# 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
|
||||
# editing this file be aware that the upstream Cargo.toml
|
||||
# will likely look very different (and much more reasonable)
|
||||
# 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]
|
||||
edition = "2018"
|
||||
rust-version = "1.31"
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
version = "1.7.0"
|
||||
authors = ["bluss"]
|
||||
description = "The enum `Either` with variants `Left` and `Right` is a general purpose sum type with two cases.\n"
|
||||
description = """
|
||||
The enum `Either` with variants `Left` and `Right` is a general purpose sum type with two cases.
|
||||
"""
|
||||
documentation = "https://docs.rs/either/1/"
|
||||
readme = "README-crates.io.md"
|
||||
keywords = ["data-structure", "no_std"]
|
||||
categories = ["data-structures", "no-std"]
|
||||
keywords = [
|
||||
"data-structure",
|
||||
"no_std",
|
||||
]
|
||||
categories = [
|
||||
"data-structures",
|
||||
"no-std",
|
||||
]
|
||||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/bluss/either"
|
||||
[package.metadata.docs.rs]
|
||||
features = ["serde"]
|
||||
|
||||
[package.metadata.release]
|
||||
no-dev-version = true
|
||||
tag-name = "{{version}}"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["serde"]
|
||||
|
||||
[dependencies.serde]
|
||||
version = "1.0"
|
||||
features = ["derive"]
|
||||
optional = true
|
||||
|
||||
[dev-dependencies.serde_json]
|
||||
version = "1.0.0"
|
||||
|
||||
|
|
|
@ -16,21 +16,41 @@ __ https://docs.rs/either/
|
|||
|
||||
|build_status|_ |crates|_
|
||||
|
||||
.. |build_status| image:: https://travis-ci.org/bluss/either.svg?branch=master
|
||||
.. _build_status: https://travis-ci.org/bluss/either
|
||||
.. |build_status| image:: https://github.com/bluss/either/workflows/CI/badge.svg?branch=master
|
||||
.. _build_status: https://github.com/bluss/either/actions
|
||||
|
||||
.. |crates| image:: http://meritbadge.herokuapp.com/either
|
||||
.. |crates| image:: https://img.shields.io/crates/v/either.svg
|
||||
.. _crates: https://crates.io/crates/either
|
||||
|
||||
How to use with cargo::
|
||||
|
||||
[dependencies]
|
||||
either = "1.6"
|
||||
either = "1.7"
|
||||
|
||||
|
||||
Recent Changes
|
||||
--------------
|
||||
|
||||
- 1.7.0
|
||||
|
||||
- **MSRV**: ``either`` now requires Rust 1.31 or later.
|
||||
|
||||
- Export the macro ``for_both!``, by @thomaseizinger (#58)
|
||||
|
||||
- Implement the ``io::Seek`` trait, by @Kerollmops (#60)
|
||||
|
||||
- Add new method ``.either_into()`` for ``Into`` conversion, by @TonalidadeHidrica (#63)
|
||||
|
||||
- Add new methods ``.factor_ok()``, ``.factor_err()``, and ``.factor_none()``,
|
||||
by @zachs18 (#67)
|
||||
|
||||
- Specialize ``source`` in the ``Error`` implementation, by @thomaseizinger (#69)
|
||||
|
||||
- Specialize more iterator methods and implement the ``FusedIterator`` trait,
|
||||
by @Ten0 (#66) and @cuviper (#71)
|
||||
|
||||
- Specialize ``Clone::clone_from``, by @cuviper (#72)
|
||||
|
||||
- 1.6.1
|
||||
|
||||
- Add new methods ``.expect_left()``, ``.unwrap_left()``,
|
||||
|
@ -123,7 +143,7 @@ License
|
|||
Dual-licensed to be compatible with the Rust project.
|
||||
|
||||
Licensed under the Apache License, Version 2.0
|
||||
http://www.apache.org/licenses/LICENSE-2.0 or the MIT license
|
||||
http://opensource.org/licenses/MIT, at your
|
||||
https://www.apache.org/licenses/LICENSE-2.0 or the MIT license
|
||||
https://opensource.org/licenses/MIT, at your
|
||||
option. This file may not be copied, modified, or distributed
|
||||
except according to those terms.
|
||||
|
|
|
@ -13,13 +13,10 @@
|
|||
//!
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/either/1/")]
|
||||
#![cfg_attr(all(not(test), not(feature = "use_std")), no_std)]
|
||||
#[cfg(all(not(test), not(feature = "use_std")))]
|
||||
extern crate core as std;
|
||||
#![no_std]
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
extern crate std;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
pub mod serde_untagged;
|
||||
|
@ -27,18 +24,18 @@ pub mod serde_untagged;
|
|||
#[cfg(feature = "serde")]
|
||||
pub mod serde_untagged_optional;
|
||||
|
||||
use std::convert::{AsMut, AsRef};
|
||||
use std::fmt;
|
||||
use std::iter;
|
||||
use std::ops::Deref;
|
||||
use std::ops::DerefMut;
|
||||
use core::convert::{AsMut, AsRef};
|
||||
use core::fmt;
|
||||
use core::iter;
|
||||
use core::ops::Deref;
|
||||
use core::ops::DerefMut;
|
||||
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
use std::error::Error;
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
use std::io::{self, BufRead, Read, Write};
|
||||
use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
|
||||
|
||||
pub use Either::{Left, Right};
|
||||
pub use crate::Either::{Left, Right};
|
||||
|
||||
/// The enum `Either` with variants `Left` and `Right` is a general purpose
|
||||
/// sum type with two cases.
|
||||
|
@ -46,8 +43,8 @@ pub use Either::{Left, Right};
|
|||
/// The `Either` type is symmetric and treats its variants the same way, without
|
||||
/// preference.
|
||||
/// (For representing success or error, use the regular `Result` enum instead.)
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
pub enum Either<L, R> {
|
||||
/// A value of type `L`.
|
||||
Left(L),
|
||||
|
@ -55,11 +52,36 @@ pub enum Either<L, R> {
|
|||
Right(R),
|
||||
}
|
||||
|
||||
macro_rules! either {
|
||||
/// Evaluate the provided expression for both [`Either::Left`] and [`Either::Right`].
|
||||
///
|
||||
/// This macro is useful in cases where both sides of [`Either`] can be interacted with
|
||||
/// in the same way even though the don't share the same type.
|
||||
///
|
||||
/// Syntax: `either::for_both!(` *expression* `,` *pattern* `=>` *expression* `)`
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use either::Either;
|
||||
///
|
||||
/// fn length(owned_or_borrowed: Either<String, &'static str>) -> usize {
|
||||
/// either::for_both!(owned_or_borrowed, s => s.len())
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let borrowed = Either::Right("Hello world!");
|
||||
/// let owned = Either::Left("Hello world!".to_owned());
|
||||
///
|
||||
/// assert_eq!(length(borrowed), 12);
|
||||
/// assert_eq!(length(owned), 12);
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! for_both {
|
||||
($value:expr, $pattern:pat => $result:expr) => {
|
||||
match $value {
|
||||
Either::Left($pattern) => $result,
|
||||
Either::Right($pattern) => $result,
|
||||
$crate::Either::Left($pattern) => $result,
|
||||
$crate::Either::Right($pattern) => $result,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -74,11 +96,10 @@ macro_rules! either {
|
|||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use] extern crate either;
|
||||
/// use either::{Either, Left, Right};
|
||||
///
|
||||
/// fn twice(wrapper: Either<u32, &str>) -> Either<u32, &str> {
|
||||
/// let value = try_left!(wrapper);
|
||||
/// let value = either::try_left!(wrapper);
|
||||
/// Left(value * 2)
|
||||
/// }
|
||||
///
|
||||
|
@ -92,7 +113,7 @@ macro_rules! try_left {
|
|||
($expr:expr) => {
|
||||
match $expr {
|
||||
$crate::Left(val) => val,
|
||||
$crate::Right(err) => return $crate::Right(::std::convert::From::from(err)),
|
||||
$crate::Right(err) => return $crate::Right(::core::convert::From::from(err)),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -102,12 +123,29 @@ macro_rules! try_left {
|
|||
macro_rules! try_right {
|
||||
($expr:expr) => {
|
||||
match $expr {
|
||||
$crate::Left(err) => return $crate::Left(::std::convert::From::from(err)),
|
||||
$crate::Left(err) => return $crate::Left(::core::convert::From::from(err)),
|
||||
$crate::Right(val) => val,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl<L: Clone, R: Clone> Clone for Either<L, R> {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Left(inner) => Left(inner.clone()),
|
||||
Right(inner) => Right(inner.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_from(&mut self, source: &Self) {
|
||||
match (self, source) {
|
||||
(Left(dest), Left(source)) => dest.clone_from(source),
|
||||
(Right(dest), Right(source)) => dest.clone_from(source),
|
||||
(dest, source) => *dest = source.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R> Either<L, R> {
|
||||
/// Return true if the value is the `Left` variant.
|
||||
///
|
||||
|
@ -388,6 +426,7 @@ impl<L, R> Either<L, R> {
|
|||
/// right.extend(left.into_iter());
|
||||
/// assert_eq!(right, Right(vec![1, 2, 3, 4, 5]));
|
||||
/// ```
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn into_iter(self) -> Either<L::IntoIter, R::IntoIter>
|
||||
where
|
||||
L: IntoIterator,
|
||||
|
@ -558,7 +597,7 @@ impl<L, R> Either<L, R> {
|
|||
/// ```
|
||||
pub fn unwrap_left(self) -> L
|
||||
where
|
||||
R: std::fmt::Debug,
|
||||
R: core::fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Either::Left(l) => l,
|
||||
|
@ -589,7 +628,7 @@ impl<L, R> Either<L, R> {
|
|||
/// ```
|
||||
pub fn unwrap_right(self) -> R
|
||||
where
|
||||
L: std::fmt::Debug,
|
||||
L: core::fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Either::Right(r) => r,
|
||||
|
@ -618,7 +657,7 @@ impl<L, R> Either<L, R> {
|
|||
/// ```
|
||||
pub fn expect_left(self, msg: &str) -> L
|
||||
where
|
||||
R: std::fmt::Debug,
|
||||
R: core::fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Either::Left(l) => l,
|
||||
|
@ -647,13 +686,103 @@ impl<L, R> Either<L, R> {
|
|||
/// ```
|
||||
pub fn expect_right(self, msg: &str) -> R
|
||||
where
|
||||
L: std::fmt::Debug,
|
||||
L: core::fmt::Debug,
|
||||
{
|
||||
match self {
|
||||
Either::Right(r) => r,
|
||||
Either::Left(l) => panic!("{}: {:?}", msg, l),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the contained value into `T`
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use either::*;
|
||||
/// // Both u16 and u32 can be converted to u64.
|
||||
/// let left: Either<u16, u32> = Left(3u16);
|
||||
/// assert_eq!(left.either_into::<u64>(), 3u64);
|
||||
/// let right: Either<u16, u32> = Right(7u32);
|
||||
/// assert_eq!(right.either_into::<u64>(), 7u64);
|
||||
/// ```
|
||||
pub fn either_into<T>(self) -> T
|
||||
where
|
||||
L: Into<T>,
|
||||
R: Into<T>,
|
||||
{
|
||||
match self {
|
||||
Either::Left(l) => l.into(),
|
||||
Either::Right(r) => r.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R> Either<Option<L>, Option<R>> {
|
||||
/// Factors out `None` from an `Either` of [`Option`].
|
||||
///
|
||||
/// ```
|
||||
/// use either::*;
|
||||
/// let left: Either<_, Option<String>> = Left(Some(vec![0]));
|
||||
/// assert_eq!(left.factor_none(), Some(Left(vec![0])));
|
||||
///
|
||||
/// let right: Either<Option<Vec<u8>>, _> = Right(Some(String::new()));
|
||||
/// assert_eq!(right.factor_none(), Some(Right(String::new())));
|
||||
/// ```
|
||||
// TODO(MSRV): doc(alias) was stabilized in Rust 1.48
|
||||
// #[doc(alias = "transpose")]
|
||||
pub fn factor_none(self) -> Option<Either<L, R>> {
|
||||
match self {
|
||||
Left(l) => l.map(Either::Left),
|
||||
Right(r) => r.map(Either::Right),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R, E> Either<Result<L, E>, Result<R, E>> {
|
||||
/// Factors out a homogenous type from an `Either` of [`Result`].
|
||||
///
|
||||
/// Here, the homogeneous type is the `Err` type of the [`Result`].
|
||||
///
|
||||
/// ```
|
||||
/// use either::*;
|
||||
/// let left: Either<_, Result<String, u32>> = Left(Ok(vec![0]));
|
||||
/// assert_eq!(left.factor_err(), Ok(Left(vec![0])));
|
||||
///
|
||||
/// let right: Either<Result<Vec<u8>, u32>, _> = Right(Ok(String::new()));
|
||||
/// assert_eq!(right.factor_err(), Ok(Right(String::new())));
|
||||
/// ```
|
||||
// TODO(MSRV): doc(alias) was stabilized in Rust 1.48
|
||||
// #[doc(alias = "transpose")]
|
||||
pub fn factor_err(self) -> Result<Either<L, R>, E> {
|
||||
match self {
|
||||
Left(l) => l.map(Either::Left),
|
||||
Right(r) => r.map(Either::Right),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, L, R> Either<Result<T, L>, Result<T, R>> {
|
||||
/// Factors out a homogenous type from an `Either` of [`Result`].
|
||||
///
|
||||
/// Here, the homogeneous type is the `Ok` type of the [`Result`].
|
||||
///
|
||||
/// ```
|
||||
/// use either::*;
|
||||
/// let left: Either<_, Result<u32, String>> = Left(Err(vec![0]));
|
||||
/// assert_eq!(left.factor_ok(), Err(Left(vec![0])));
|
||||
///
|
||||
/// let right: Either<Result<u32, Vec<u8>>, _> = Right(Err(String::new()));
|
||||
/// assert_eq!(right.factor_ok(), Err(Right(String::new())));
|
||||
/// ```
|
||||
// TODO(MSRV): doc(alias) was stabilized in Rust 1.48
|
||||
// #[doc(alias = "transpose")]
|
||||
pub fn factor_ok(self) -> Result<T, Either<L, R>> {
|
||||
match self {
|
||||
Left(l) => l.map_err(Either::Left),
|
||||
Right(r) => r.map_err(Either::Right),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, L, R> Either<(T, L), (T, R)> {
|
||||
|
@ -711,7 +840,7 @@ impl<T> Either<T, T> {
|
|||
/// assert_eq!(right.into_inner(), 123);
|
||||
/// ```
|
||||
pub fn into_inner(self) -> T {
|
||||
either!(self, inner => inner)
|
||||
for_both!(self, inner => inner)
|
||||
}
|
||||
|
||||
/// Map `f` over the contained value and return the result in the
|
||||
|
@ -747,6 +876,7 @@ impl<L, R> From<Result<R, L>> for Either<L, R> {
|
|||
}
|
||||
|
||||
/// Convert from `Either` to `Result` with `Right => Ok` and `Left => Err`.
|
||||
#[allow(clippy::from_over_into)] // From requires RFC 2451, Rust 1.41
|
||||
impl<L, R> Into<Result<R, L>> for Either<L, R> {
|
||||
fn into(self) -> Result<R, L> {
|
||||
match self {
|
||||
|
@ -765,7 +895,7 @@ where
|
|||
where
|
||||
T: IntoIterator<Item = A>,
|
||||
{
|
||||
either!(*self, ref mut inner => inner.extend(iter))
|
||||
for_both!(*self, ref mut inner => inner.extend(iter))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -778,44 +908,87 @@ where
|
|||
type Item = L::Item;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
either!(*self, ref mut inner => inner.next())
|
||||
for_both!(*self, ref mut inner => inner.next())
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
either!(*self, ref inner => inner.size_hint())
|
||||
for_both!(*self, ref inner => inner.size_hint())
|
||||
}
|
||||
|
||||
fn fold<Acc, G>(self, init: Acc, f: G) -> Acc
|
||||
where
|
||||
G: FnMut(Acc, Self::Item) -> Acc,
|
||||
{
|
||||
either!(self, inner => inner.fold(init, f))
|
||||
for_both!(self, inner => inner.fold(init, f))
|
||||
}
|
||||
|
||||
fn for_each<F>(self, f: F)
|
||||
where
|
||||
F: FnMut(Self::Item),
|
||||
{
|
||||
for_both!(self, inner => inner.for_each(f))
|
||||
}
|
||||
|
||||
fn count(self) -> usize {
|
||||
either!(self, inner => inner.count())
|
||||
for_both!(self, inner => inner.count())
|
||||
}
|
||||
|
||||
fn last(self) -> Option<Self::Item> {
|
||||
either!(self, inner => inner.last())
|
||||
for_both!(self, inner => inner.last())
|
||||
}
|
||||
|
||||
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
||||
either!(*self, ref mut inner => inner.nth(n))
|
||||
for_both!(*self, ref mut inner => inner.nth(n))
|
||||
}
|
||||
|
||||
fn collect<B>(self) -> B
|
||||
where
|
||||
B: iter::FromIterator<Self::Item>,
|
||||
{
|
||||
either!(self, inner => inner.collect())
|
||||
for_both!(self, inner => inner.collect())
|
||||
}
|
||||
|
||||
fn partition<B, F>(self, f: F) -> (B, B)
|
||||
where
|
||||
B: Default + Extend<Self::Item>,
|
||||
F: FnMut(&Self::Item) -> bool,
|
||||
{
|
||||
for_both!(self, inner => inner.partition(f))
|
||||
}
|
||||
|
||||
fn all<F>(&mut self, f: F) -> bool
|
||||
where
|
||||
F: FnMut(Self::Item) -> bool,
|
||||
{
|
||||
either!(*self, ref mut inner => inner.all(f))
|
||||
for_both!(*self, ref mut inner => inner.all(f))
|
||||
}
|
||||
|
||||
fn any<F>(&mut self, f: F) -> bool
|
||||
where
|
||||
F: FnMut(Self::Item) -> bool,
|
||||
{
|
||||
for_both!(*self, ref mut inner => inner.any(f))
|
||||
}
|
||||
|
||||
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
|
||||
where
|
||||
P: FnMut(&Self::Item) -> bool,
|
||||
{
|
||||
for_both!(*self, ref mut inner => inner.find(predicate))
|
||||
}
|
||||
|
||||
fn find_map<B, F>(&mut self, f: F) -> Option<B>
|
||||
where
|
||||
F: FnMut(Self::Item) -> Option<B>,
|
||||
{
|
||||
for_both!(*self, ref mut inner => inner.find_map(f))
|
||||
}
|
||||
|
||||
fn position<P>(&mut self, predicate: P) -> Option<usize>
|
||||
where
|
||||
P: FnMut(Self::Item) -> bool,
|
||||
{
|
||||
for_both!(*self, ref mut inner => inner.position(predicate))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -825,7 +998,26 @@ where
|
|||
R: DoubleEndedIterator<Item = L::Item>,
|
||||
{
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
either!(*self, ref mut inner => inner.next_back())
|
||||
for_both!(*self, ref mut inner => inner.next_back())
|
||||
}
|
||||
|
||||
// TODO(MSRV): This was stabilized in Rust 1.37
|
||||
// fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
|
||||
// for_both!(*self, ref mut inner => inner.nth_back(n))
|
||||
// }
|
||||
|
||||
fn rfold<Acc, G>(self, init: Acc, f: G) -> Acc
|
||||
where
|
||||
G: FnMut(Acc, Self::Item) -> Acc,
|
||||
{
|
||||
for_both!(self, inner => inner.rfold(init, f))
|
||||
}
|
||||
|
||||
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
|
||||
where
|
||||
P: FnMut(&Self::Item) -> bool,
|
||||
{
|
||||
for_both!(*self, ref mut inner => inner.rfind(predicate))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -833,6 +1025,16 @@ impl<L, R> ExactSizeIterator for Either<L, R>
|
|||
where
|
||||
L: ExactSizeIterator,
|
||||
R: ExactSizeIterator<Item = L::Item>,
|
||||
{
|
||||
fn len(&self) -> usize {
|
||||
for_both!(*self, ref inner => inner.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R> iter::FusedIterator for Either<L, R>
|
||||
where
|
||||
L: iter::FusedIterator,
|
||||
R: iter::FusedIterator<Item = L::Item>,
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -846,11 +1048,25 @@ where
|
|||
R: Read,
|
||||
{
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
either!(*self, ref mut inner => inner.read(buf))
|
||||
for_both!(*self, ref mut inner => inner.read(buf))
|
||||
}
|
||||
|
||||
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
||||
either!(*self, ref mut inner => inner.read_to_end(buf))
|
||||
fn read_to_end(&mut self, buf: &mut std::vec::Vec<u8>) -> io::Result<usize> {
|
||||
for_both!(*self, ref mut inner => inner.read_to_end(buf))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
/// `Either<L, R>` implements `Seek` if both `L` and `R` do.
|
||||
///
|
||||
/// Requires crate feature `"use_std"`
|
||||
impl<L, R> Seek for Either<L, R>
|
||||
where
|
||||
L: Seek,
|
||||
R: Seek,
|
||||
{
|
||||
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||
for_both!(*self, ref mut inner => inner.seek(pos))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -862,11 +1078,11 @@ where
|
|||
R: BufRead,
|
||||
{
|
||||
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
||||
either!(*self, ref mut inner => inner.fill_buf())
|
||||
for_both!(*self, ref mut inner => inner.fill_buf())
|
||||
}
|
||||
|
||||
fn consume(&mut self, amt: usize) {
|
||||
either!(*self, ref mut inner => inner.consume(amt))
|
||||
for_both!(*self, ref mut inner => inner.consume(amt))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -880,11 +1096,11 @@ where
|
|||
R: Write,
|
||||
{
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
either!(*self, ref mut inner => inner.write(buf))
|
||||
for_both!(*self, ref mut inner => inner.write(buf))
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
either!(*self, ref mut inner => inner.flush())
|
||||
for_both!(*self, ref mut inner => inner.flush())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -894,7 +1110,7 @@ where
|
|||
R: AsRef<Target>,
|
||||
{
|
||||
fn as_ref(&self) -> &Target {
|
||||
either!(*self, ref inner => inner.as_ref())
|
||||
for_both!(*self, ref inner => inner.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -905,7 +1121,7 @@ macro_rules! impl_specific_ref_and_mut {
|
|||
where L: AsRef<$t>, R: AsRef<$t>
|
||||
{
|
||||
fn as_ref(&self) -> &$t {
|
||||
either!(*self, ref inner => inner.as_ref())
|
||||
for_both!(*self, ref inner => inner.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -914,7 +1130,7 @@ macro_rules! impl_specific_ref_and_mut {
|
|||
where L: AsMut<$t>, R: AsMut<$t>
|
||||
{
|
||||
fn as_mut(&mut self) -> &mut $t {
|
||||
either!(*self, ref mut inner => inner.as_mut())
|
||||
for_both!(*self, ref mut inner => inner.as_mut())
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -943,7 +1159,7 @@ where
|
|||
R: AsRef<[Target]>,
|
||||
{
|
||||
fn as_ref(&self) -> &[Target] {
|
||||
either!(*self, ref inner => inner.as_ref())
|
||||
for_both!(*self, ref inner => inner.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -953,7 +1169,7 @@ where
|
|||
R: AsMut<Target>,
|
||||
{
|
||||
fn as_mut(&mut self) -> &mut Target {
|
||||
either!(*self, ref mut inner => inner.as_mut())
|
||||
for_both!(*self, ref mut inner => inner.as_mut())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -963,7 +1179,7 @@ where
|
|||
R: AsMut<[Target]>,
|
||||
{
|
||||
fn as_mut(&mut self) -> &mut [Target] {
|
||||
either!(*self, ref mut inner => inner.as_mut())
|
||||
for_both!(*self, ref mut inner => inner.as_mut())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -975,7 +1191,7 @@ where
|
|||
type Target = L::Target;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
either!(*self, ref inner => &*inner)
|
||||
for_both!(*self, ref inner => &**inner)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -985,7 +1201,7 @@ where
|
|||
R: DerefMut<Target = L::Target>,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
either!(*self, ref mut inner => &mut *inner)
|
||||
for_both!(*self, ref mut inner => &mut *inner)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -996,15 +1212,18 @@ where
|
|||
L: Error,
|
||||
R: Error,
|
||||
{
|
||||
#[allow(deprecated)]
|
||||
fn description(&self) -> &str {
|
||||
either!(*self, ref inner => inner.description())
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
for_both!(*self, ref inner => inner.source())
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[allow(unknown_lints, bare_trait_objects)]
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
either!(*self, ref inner => inner.cause())
|
||||
fn description(&self) -> &str {
|
||||
for_both!(*self, ref inner => inner.description())
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
fn cause(&self) -> Option<&dyn Error> {
|
||||
for_both!(*self, ref inner => inner.cause())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1013,8 +1232,8 @@ where
|
|||
L: fmt::Display,
|
||||
R: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
either!(*self, ref inner => inner.fmt(f))
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for_both!(*self, ref inner => inner.fmt(f))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1033,6 +1252,8 @@ fn basic() {
|
|||
|
||||
#[test]
|
||||
fn macros() {
|
||||
use std::string::String;
|
||||
|
||||
fn a() -> Either<u32, u32> {
|
||||
let x: u32 = try_left!(Right(1337u32));
|
||||
Left(x * 2)
|
||||
|
@ -1047,6 +1268,8 @@ fn macros() {
|
|||
|
||||
#[test]
|
||||
fn deref() {
|
||||
use std::string::String;
|
||||
|
||||
fn is_str(_: &str) {}
|
||||
let value: Either<String, &str> = Left(String::from("test"));
|
||||
is_str(&*value);
|
||||
|
@ -1064,6 +1287,37 @@ fn iter() {
|
|||
assert_eq!(iter.count(), 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seek() {
|
||||
use std::io;
|
||||
|
||||
let use_empty = false;
|
||||
let mut mockdata = [0x00; 256];
|
||||
for i in 0..256 {
|
||||
mockdata[i] = i as u8;
|
||||
}
|
||||
|
||||
let mut reader = if use_empty {
|
||||
// Empty didn't impl Seek until Rust 1.51
|
||||
Left(io::Cursor::new([]))
|
||||
} else {
|
||||
Right(io::Cursor::new(&mockdata[..]))
|
||||
};
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
|
||||
assert_eq!(buf, mockdata[..buf.len()]);
|
||||
|
||||
// the first read should advance the cursor and return the next 16 bytes thus the `ne`
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
|
||||
assert_ne!(buf, mockdata[..buf.len()]);
|
||||
|
||||
// if the seek operation fails it should read 16..31 instead of 0..15
|
||||
reader.seek(io::SeekFrom::Start(0)).unwrap();
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
|
||||
assert_eq!(buf, mockdata[..buf.len()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_write() {
|
||||
use std::io;
|
||||
|
|
|
@ -6,15 +6,11 @@
|
|||
//! but in typical cases Vec<String> would suffice, too.
|
||||
//!
|
||||
//! ```rust
|
||||
//! #[macro_use]
|
||||
//! extern crate serde;
|
||||
//! // or `use serde::{Serialize, Deserialize};` in newer rust versions.
|
||||
//!
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! use either::Either;
|
||||
//! use std::collections::HashMap;
|
||||
//!
|
||||
//! #[derive(Serialize, Deserialize, Debug)]
|
||||
//! #[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||
//! #[serde(transparent)]
|
||||
//! struct IntOrString {
|
||||
//! #[serde(with = "either::serde_untagged")]
|
||||
|
@ -39,7 +35,7 @@
|
|||
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Either<L, R> {
|
||||
Left(L),
|
||||
|
@ -53,8 +49,8 @@ where
|
|||
R: Serialize,
|
||||
{
|
||||
let untagged = match this {
|
||||
&super::Either::Left(ref left) => Either::Left(left),
|
||||
&super::Either::Right(ref right) => Either::Right(right),
|
||||
super::Either::Left(left) => Either::Left(left),
|
||||
super::Either::Right(right) => Either::Right(right),
|
||||
};
|
||||
untagged.serialize(serializer)
|
||||
}
|
||||
|
|
|
@ -6,15 +6,11 @@
|
|||
//! but in typical cases Vec<String> would suffice, too.
|
||||
//!
|
||||
//! ```rust
|
||||
//! #[macro_use]
|
||||
//! extern crate serde;
|
||||
//! // or `use serde::{Serialize, Deserialize};` in newer rust versions.
|
||||
//!
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! use either::Either;
|
||||
//! use std::collections::HashMap;
|
||||
//!
|
||||
//! #[derive(Serialize, Deserialize, Debug)]
|
||||
//! #[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||
//! #[serde(transparent)]
|
||||
//! struct IntOrString {
|
||||
//! #[serde(with = "either::serde_untagged_optional")]
|
||||
|
@ -56,9 +52,9 @@ where
|
|||
R: Serialize,
|
||||
{
|
||||
let untagged = match this {
|
||||
&Some(super::Either::Left(ref left)) => Some(Either::Left(left)),
|
||||
&Some(super::Either::Right(ref right)) => Some(Either::Right(right)),
|
||||
&None => None,
|
||||
Some(super::Either::Left(left)) => Some(Either::Left(left)),
|
||||
Some(super::Either::Right(right)) => Some(Either::Right(right)),
|
||||
None => None,
|
||||
};
|
||||
untagged.serialize(serializer)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче