From 6ca693278a1d04d2ca698e6cb6862ada6cf3d28e Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 24 Jun 2022 15:19:35 -0700 Subject: [PATCH 1/4] Update to 2018 edition --- Cargo.toml | 2 ++ src/lib.rs | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5797caf..90ae914 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,8 @@ name = "either" version = "1.6.1" authors = ["bluss"] +edition = "2018" +rust-version = "1.31" license = "MIT/Apache-2.0" repository = "https://github.com/bluss/either" diff --git a/src/lib.rs b/src/lib.rs index 3162abb..2b2eb15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ use std::error::Error; #[cfg(any(test, feature = "use_std"))] 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. @@ -1069,8 +1069,7 @@ where } #[allow(deprecated)] - #[allow(unknown_lints, bare_trait_objects)] - fn cause(&self) -> Option<&Error> { + fn cause(&self) -> Option<&dyn Error> { for_both!(*self, ref inner => inner.cause()) } } @@ -1080,7 +1079,7 @@ where L: fmt::Display, R: fmt::Display, { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for_both!(*self, ref inner => inner.fmt(f)) } } From 3f16687a1974559cb1f957783953fb285f494d3c Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 24 Jun 2022 15:41:40 -0700 Subject: [PATCH 2/4] Clean up no_std --- .github/workflows/ci.yml | 6 ++++++ src/lib.rs | 35 ++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f89d371..a87a3b5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,12 @@ jobs: cargo generate-lockfile cargo update -p serde_json --precise 1.0.39 + - name: Build (no_std) + uses: actions-rs/cargo@v1 + with: + command: build + args: --no-default-features + - name: Build uses: actions-rs/cargo@v1 with: diff --git a/src/lib.rs b/src/lib.rs index 2b2eb15..08d4f2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,9 +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(any(test, feature = "use_std"))] +extern crate std; #[cfg(feature = "serde")] #[macro_use] @@ -27,11 +28,11 @@ 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; @@ -118,7 +119,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)), } }; } @@ -128,7 +129,7 @@ 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, } }; @@ -584,7 +585,7 @@ impl Either { /// ``` pub fn unwrap_left(self) -> L where - R: std::fmt::Debug, + R: core::fmt::Debug, { match self { Either::Left(l) => l, @@ -615,7 +616,7 @@ impl Either { /// ``` pub fn unwrap_right(self) -> R where - L: std::fmt::Debug, + L: core::fmt::Debug, { match self { Either::Right(r) => r, @@ -644,7 +645,7 @@ impl Either { /// ``` pub fn expect_left(self, msg: &str) -> L where - R: std::fmt::Debug, + R: core::fmt::Debug, { match self { Either::Left(l) => l, @@ -673,7 +674,7 @@ impl Either { /// ``` pub fn expect_right(self, msg: &str) -> R where - L: std::fmt::Debug, + L: core::fmt::Debug, { match self { Either::Right(r) => r, @@ -898,7 +899,7 @@ where for_both!(*self, ref mut inner => inner.read(buf)) } - fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + fn read_to_end(&mut self, buf: &mut std::vec::Vec) -> io::Result { for_both!(*self, ref mut inner => inner.read_to_end(buf)) } } @@ -1099,6 +1100,8 @@ fn basic() { #[test] fn macros() { + use std::string::String; + fn a() -> Either { let x: u32 = try_left!(Right(1337u32)); Left(x * 2) @@ -1113,6 +1116,8 @@ fn macros() { #[test] fn deref() { + use std::string::String; + fn is_str(_: &str) {} let value: Either = Left(String::from("test")); is_str(&*value); From edffc52d2391d72fd0cffab05dd866c73b56e0dd Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 24 Jun 2022 15:45:59 -0700 Subject: [PATCH 3/4] Stop using extern crate where possible --- src/lib.rs | 12 +++--------- src/serde_untagged.rs | 9 ++------- src/serde_untagged_optional.rs | 7 +------ 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 08d4f2b..99e1b49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,10 +18,6 @@ #[cfg(any(test, feature = "use_std"))] extern crate std; -#[cfg(feature = "serde")] -#[macro_use] -extern crate serde; - #[cfg(feature = "serde")] pub mod serde_untagged; @@ -47,7 +43,7 @@ pub use crate::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))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub enum Either { /// A value of type `L`. @@ -66,11 +62,10 @@ pub enum Either { /// # Example /// /// ``` -/// #[macro_use] extern crate either; /// use either::Either; /// /// fn length(owned_or_borrowed: Either) -> usize { -/// for_both!(owned_or_borrowed, s => s.len()) +/// either::for_both!(owned_or_borrowed, s => s.len()) /// } /// /// fn main() { @@ -101,11 +96,10 @@ macro_rules! for_both { /// # Example /// /// ``` -/// #[macro_use] extern crate either; /// use either::{Either, Left, Right}; /// /// fn twice(wrapper: Either) -> Either { -/// let value = try_left!(wrapper); +/// let value = either::try_left!(wrapper); /// Left(value * 2) /// } /// diff --git a/src/serde_untagged.rs b/src/serde_untagged.rs index 5da36b6..610e8ed 100644 --- a/src/serde_untagged.rs +++ b/src/serde_untagged.rs @@ -6,16 +6,11 @@ //! but in typical cases Vec would suffice, too. //! //! ```rust -//! extern crate either; -//! #[macro_use] -//! extern crate serde; -//! // or `use serde::{Serialize, Deserialize};` in newer rust versions. -//! //! # fn main() -> Result<(), Box> { //! 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")] @@ -40,7 +35,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[derive(Serialize, Deserialize)] +#[derive(serde::Serialize, serde::Deserialize)] #[serde(untagged)] enum Either { Left(L), diff --git a/src/serde_untagged_optional.rs b/src/serde_untagged_optional.rs index 2905606..9cd00da 100644 --- a/src/serde_untagged_optional.rs +++ b/src/serde_untagged_optional.rs @@ -6,16 +6,11 @@ //! but in typical cases Vec would suffice, too. //! //! ```rust -//! extern crate either; -//! #[macro_use] -//! extern crate serde; -//! // or `use serde::{Serialize, Deserialize};` in newer rust versions. -//! //! # fn main() -> Result<(), Box> { //! 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")] From d451f5c5287abb2d897188a13d4af0c09cdd9b11 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 24 Jun 2022 16:03:06 -0700 Subject: [PATCH 4/4] Fix clippy warnings --- src/lib.rs | 10 ++++++---- src/serde_untagged.rs | 4 ++-- src/serde_untagged_optional.rs | 6 +++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 99e1b49..1268e6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -409,6 +409,7 @@ impl Either { /// 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 where L: IntoIterator, @@ -791,6 +792,7 @@ impl From> for Either { } /// Convert from `Either` to `Result` with `Right => Ok` and `Left => Err`. +#[allow(clippy::from_over_into)] // From requires RFC 2451, Rust 1.41 impl Into> for Either { fn into(self) -> Result { match self { @@ -1033,7 +1035,7 @@ where type Target = L::Target; fn deref(&self) -> &Self::Target { - for_both!(*self, ref inner => &*inner) + for_both!(*self, ref inner => &**inner) } } @@ -1148,16 +1150,16 @@ fn seek() { let mut buf = [0u8; 16]; assert_eq!(reader.read(&mut buf).unwrap(), buf.len()); - assert_eq!(&buf, &mockdata[..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!(&buf != &mockdata[..buf.len()]); // (assert_ne needs Rust 1.13) + 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()]); + assert_eq!(buf, mockdata[..buf.len()]); } #[test] diff --git a/src/serde_untagged.rs b/src/serde_untagged.rs index 610e8ed..72078c3 100644 --- a/src/serde_untagged.rs +++ b/src/serde_untagged.rs @@ -49,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) } diff --git a/src/serde_untagged_optional.rs b/src/serde_untagged_optional.rs index 9cd00da..fb3239a 100644 --- a/src/serde_untagged_optional.rs +++ b/src/serde_untagged_optional.rs @@ -52,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) }