Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publicly expose either macro as for_both #58

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 55 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,35 @@ 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,
}
};
}
Expand Down Expand Up @@ -711,7 +735,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
Expand Down Expand Up @@ -765,7 +789,7 @@ where
where
T: IntoIterator<Item = A>,
{
either!(*self, ref mut inner => inner.extend(iter))
for_both!(*self, ref mut inner => inner.extend(iter))
}
}

Expand All @@ -778,44 +802,44 @@ 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 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 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))
}
}

Expand All @@ -825,7 +849,7 @@ 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())
}
}

Expand All @@ -846,11 +870,11 @@ 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))
for_both!(*self, ref mut inner => inner.read_to_end(buf))
}
}

Expand All @@ -862,11 +886,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))
}
}

Expand All @@ -880,11 +904,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())
}
}

Expand All @@ -894,7 +918,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())
}
}

Expand All @@ -905,7 +929,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())
}
}

Expand All @@ -914,7 +938,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())
}
}
};
Expand Down Expand Up @@ -943,7 +967,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())
}
}

Expand All @@ -953,7 +977,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())
}
}

Expand All @@ -963,7 +987,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())
}
}

Expand All @@ -975,7 +999,7 @@ where
type Target = L::Target;

fn deref(&self) -> &Self::Target {
either!(*self, ref inner => &*inner)
for_both!(*self, ref inner => &*inner)
}
}

Expand All @@ -985,7 +1009,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)
}
}

Expand All @@ -998,13 +1022,13 @@ where
{
#[allow(deprecated)]
fn description(&self) -> &str {
either!(*self, ref inner => inner.description())
for_both!(*self, ref inner => inner.description())
}

#[allow(deprecated)]
#[allow(unknown_lints, bare_trait_objects)]
fn cause(&self) -> Option<&Error> {
either!(*self, ref inner => inner.cause())
for_both!(*self, ref inner => inner.cause())
}
}

Expand All @@ -1014,7 +1038,7 @@ where
R: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
either!(*self, ref inner => inner.fmt(f))
for_both!(*self, ref inner => inner.fmt(f))
}
}

Expand Down