changeset 46435:2e2033081274

rust: replace trivial `impl From …` with `#[derive(derive_more::From)]` Crate docs: https://jeltef.github.io/derive_more/derive_more/from.html Differential Revision: https://phab.mercurial-scm.org/D9875
author Simon Sapin <simon.sapin@octobus.net>
date Tue, 26 Jan 2021 20:05:37 +0100
parents 3e2d539d0d1a
children 252d1bdba33d
files rust/Cargo.lock rust/hg-core/Cargo.toml rust/hg-core/src/config/layer.rs rust/hg-core/src/dirstate/status.rs rust/hg-core/src/lib.rs rust/hg-core/src/operations/debugdata.rs rust/hg-core/src/operations/list_tracked_files.rs rust/hg-core/src/revlog/node.rs rust/hg-core/src/utils/hg_path.rs rust/rhg/Cargo.toml rust/rhg/src/error.rs
diffstat 11 files changed, 32 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/Cargo.lock	Tue Jan 26 20:05:37 2021 +0100
@@ -199,6 +199,16 @@
 ]
 
 [[package]]
+name = "derive_more"
+version = "0.99.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 1.0.54 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "difference"
 version = "2.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -293,6 +303,7 @@
  "bytes-cast 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "crossbeam-channel 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "flate2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
  "format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "im-rc 15.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -696,6 +707,7 @@
 version = "0.1.0"
 dependencies = [
  "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "format-bytes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "hg-core 0.1.0",
@@ -939,6 +951,7 @@
 "checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
 "checksum crossbeam-utils 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d"
 "checksum ctor 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "7fbaabec2c953050352311293be5c6aba8e141ba19d6811862b232d6fd020484"
+"checksum derive_more 0.99.11 (registry+https://github.com/rust-lang/crates.io-index)" = "41cb0e6161ad61ed084a36ba71fbba9e3ac5aee3606fb607fe08da6acbcf3d8c"
 "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
 "checksum either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
--- a/rust/hg-core/Cargo.toml	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/Cargo.toml	Tue Jan 26 20:05:37 2021 +0100
@@ -11,6 +11,7 @@
 [dependencies]
 bytes-cast = "0.1"
 byteorder = "1.3.4"
+derive_more = "0.99"
 im-rc = "15.0.*"
 lazy_static = "1.4.0"
 memchr = "2.3.3"
--- a/rust/hg-core/src/config/layer.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/config/layer.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -226,7 +226,7 @@
     }
 }
 
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum ConfigError {
     Parse {
         origin: ConfigOrigin,
@@ -239,15 +239,10 @@
         io_error: std::io::Error,
     },
     /// Any IO error that isn't expected
+    #[from]
     IO(std::io::Error),
 }
 
-impl From<std::io::Error> for ConfigError {
-    fn from(e: std::io::Error) -> Self {
-        Self::IO(e)
-    }
-}
-
 fn make_regex(pattern: &'static str) -> Regex {
     Regex::new(pattern).expect("expected a valid regex")
 }
--- a/rust/hg-core/src/dirstate/status.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/dirstate/status.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -265,7 +265,7 @@
     pub traversed: Vec<HgPathBuf>,
 }
 
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum StatusError {
     /// Generic IO error
     IO(std::io::Error),
@@ -277,22 +277,6 @@
 
 pub type StatusResult<T> = Result<T, StatusError>;
 
-impl From<PatternError> for StatusError {
-    fn from(e: PatternError) -> Self {
-        StatusError::Pattern(e)
-    }
-}
-impl From<HgPathError> for StatusError {
-    fn from(e: HgPathError) -> Self {
-        StatusError::Path(e)
-    }
-}
-impl From<std::io::Error> for StatusError {
-    fn from(e: std::io::Error) -> Self {
-        StatusError::IO(e)
-    }
-}
-
 impl ToString for StatusError {
     fn to_string(&self) -> String {
         match self {
--- a/rust/hg-core/src/lib.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/lib.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -89,6 +89,7 @@
         DirstatePackError::CorruptedEntry(e.to_string())
     }
 }
+
 #[derive(Debug, PartialEq)]
 pub enum DirstateMapError {
     PathNotFound(HgPathBuf),
@@ -108,7 +109,7 @@
     }
 }
 
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum DirstateError {
     Parse(DirstateParseError),
     Pack(DirstatePackError),
@@ -116,24 +117,14 @@
     IO(std::io::Error),
 }
 
-impl From<DirstateParseError> for DirstateError {
-    fn from(e: DirstateParseError) -> Self {
-        DirstateError::Parse(e)
-    }
-}
-
-impl From<DirstatePackError> for DirstateError {
-    fn from(e: DirstatePackError) -> Self {
-        DirstateError::Pack(e)
-    }
-}
-
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum PatternError {
+    #[from]
     Path(HgPathError),
     UnsupportedSyntax(String),
     UnsupportedSyntaxInFile(String, String, usize),
     TooLong(usize),
+    #[from]
     IO(std::io::Error),
     /// Needed a pattern that can be turned into a regex but got one that
     /// can't. This should only happen through programmer error.
@@ -163,27 +154,3 @@
         }
     }
 }
-
-impl From<DirstateMapError> for DirstateError {
-    fn from(e: DirstateMapError) -> Self {
-        DirstateError::Map(e)
-    }
-}
-
-impl From<std::io::Error> for DirstateError {
-    fn from(e: std::io::Error) -> Self {
-        DirstateError::IO(e)
-    }
-}
-
-impl From<std::io::Error> for PatternError {
-    fn from(e: std::io::Error) -> Self {
-        PatternError::IO(e)
-    }
-}
-
-impl From<HgPathError> for PatternError {
-    fn from(e: HgPathError) -> Self {
-        PatternError::Path(e)
-    }
-}
--- a/rust/hg-core/src/operations/debugdata.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/operations/debugdata.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -16,9 +16,10 @@
 }
 
 /// Error type for `debug_data`
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum DebugDataError {
     /// Error when reading a `revlog` file.
+    #[from]
     IoError(std::io::Error),
     /// The revision has not been found.
     InvalidRevision,
@@ -32,12 +33,6 @@
     UnknowRevlogDataFormat(u8),
 }
 
-impl From<std::io::Error> for DebugDataError {
-    fn from(err: std::io::Error) -> Self {
-        DebugDataError::IoError(err)
-    }
-}
-
 impl From<RevlogError> for DebugDataError {
     fn from(err: RevlogError) -> Self {
         match err {
--- a/rust/hg-core/src/operations/list_tracked_files.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/operations/list_tracked_files.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -17,7 +17,7 @@
 use std::convert::From;
 
 /// Error type for `Dirstate` methods
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum ListDirstateTrackedFilesError {
     /// Error when reading the `dirstate` file
     IoError(std::io::Error),
@@ -25,12 +25,6 @@
     ParseError(DirstateParseError),
 }
 
-impl From<std::io::Error> for ListDirstateTrackedFilesError {
-    fn from(err: std::io::Error) -> Self {
-        ListDirstateTrackedFilesError::IoError(err)
-    }
-}
-
 /// List files under Mercurial control in the working directory
 /// by reading the dirstate
 pub struct Dirstate {
--- a/rust/hg-core/src/revlog/node.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/revlog/node.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -49,7 +49,7 @@
 /// the size or return an error at runtime.
 ///
 /// [`nybbles_len`]: #method.nybbles_len
-#[derive(Copy, Clone, Debug, PartialEq, BytesCast)]
+#[derive(Copy, Clone, Debug, PartialEq, BytesCast, derive_more::From)]
 #[repr(transparent)]
 pub struct Node {
     data: NodeData,
@@ -60,12 +60,6 @@
     data: [0; NODE_BYTES_LENGTH],
 };
 
-impl From<NodeData> for Node {
-    fn from(data: NodeData) -> Node {
-        Node { data }
-    }
-}
-
 /// Return an error if the slice has an unexpected length
 impl<'a> TryFrom<&'a [u8]> for &'a Node {
     type Error = ();
--- a/rust/hg-core/src/utils/hg_path.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/hg-core/src/utils/hg_path.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -367,7 +367,9 @@
     }
 }
 
-#[derive(Default, Eq, Ord, Clone, PartialEq, PartialOrd, Hash)]
+#[derive(
+    Default, Eq, Ord, Clone, PartialEq, PartialOrd, Hash, derive_more::From,
+)]
 pub struct HgPathBuf {
     inner: Vec<u8>,
 }
@@ -408,12 +410,6 @@
     }
 }
 
-impl From<Vec<u8>> for HgPathBuf {
-    fn from(vec: Vec<u8>) -> Self {
-        Self { inner: vec }
-    }
-}
-
 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
     fn from(s: &T) -> HgPathBuf {
         s.as_ref().to_owned()
--- a/rust/rhg/Cargo.toml	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/rhg/Cargo.toml	Tue Jan 26 20:05:37 2021 +0100
@@ -10,6 +10,7 @@
 [dependencies]
 hg-core = { path = "../hg-core"}
 clap = "2.33.1"
+derive_more = "0.99"
 log = "0.4.11"
 micro-timer = "0.3.1"
 env_logger = "0.7.1"
--- a/rust/rhg/src/error.rs	Tue Jan 26 19:07:24 2021 +0100
+++ b/rust/rhg/src/error.rs	Tue Jan 26 20:05:37 2021 +0100
@@ -8,13 +8,14 @@
 use std::path::PathBuf;
 
 /// The kind of command error
-#[derive(Debug)]
+#[derive(Debug, derive_more::From)]
 pub enum CommandError {
     /// The root of the repository cannot be found
     RootNotFound(PathBuf),
     /// The current directory cannot be found
     CurrentDirNotFound(std::io::Error),
     /// `.hg/requires`
+    #[from]
     RequirementsError(RequirementsError),
     /// The standard output stream cannot be written to
     StdoutError,
@@ -93,9 +94,3 @@
         }
     }
 }
-
-impl From<RequirementsError> for CommandError {
-    fn from(err: RequirementsError) -> Self {
-        CommandError::RequirementsError(err)
-    }
-}