From 2768925ec689e59025f94d64eafd25f281170b23 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 19 Feb 2021 07:41:25 -0800 Subject: [PATCH] tool: Support downcasting generic `Ec` Removes the `impl Access for &mut dyn Access` I added earlier. But that hasn't proven too useful. --- tool/Cargo.lock | 7 +++++++ tool/Cargo.toml | 3 ++- tool/src/access/mod.rs | 15 ++++----------- tool/src/ec.rs | 8 -------- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/tool/Cargo.lock b/tool/Cargo.lock index 166e70d..e20fced 100644 --- a/tool/Cargo.lock +++ b/tool/Cargo.lock @@ -42,6 +42,11 @@ dependencies = [ "vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hermit-abi" version = "0.1.18" @@ -85,6 +90,7 @@ name = "system76_ectool" version = "0.2.3" dependencies = [ "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", + "downcast-rs 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "hidapi 1.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.81 (registry+https://github.com/rust-lang/crates.io-index)", "redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -133,6 +139,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum cc 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" "checksum clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)" = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +"checksum downcast-rs 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" "checksum hermit-abi 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" "checksum hidapi 1.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "76c352a18370f7e7e47bcbfcbdc5432b8c80c705b5d751a25232c659fcf5c775" "checksum libc 0.2.81 (registry+https://github.com/rust-lang/crates.io-index)" = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" diff --git a/tool/Cargo.toml b/tool/Cargo.toml index d4f333b..214b04d 100644 --- a/tool/Cargo.toml +++ b/tool/Cargo.toml @@ -20,10 +20,11 @@ clap = "2" libc = { version = "0.2", optional = true } hidapi = { version = "1.2", default-features = false, features = ["linux-static-hidraw"], optional = true } redox_hwio = { version = "0.1.3", optional = true } +downcast-rs = { version = "1.2.0", default-features = false } [features] default = ["std", "hidapi"] -std = ["libc"] +std = ["libc", "downcast-rs/std"] [package.metadata.docs.rs] all-features = true diff --git a/tool/src/access/mod.rs b/tool/src/access/mod.rs index 297ea3c..be12207 100644 --- a/tool/src/access/mod.rs +++ b/tool/src/access/mod.rs @@ -1,4 +1,5 @@ use crate::Error; +use downcast_rs::Downcast; #[cfg(feature = "hidapi")] pub use self::hid::AccessHid; @@ -17,7 +18,7 @@ pub use self::lpc::*; mod lpc; /// Access method for running an EC command -pub trait Access { +pub trait Access: Downcast { /// Sends a command using the access method. Only internal use is recommended unsafe fn command(&mut self, cmd: u8, data: &mut [u8]) -> Result; @@ -31,16 +32,6 @@ pub trait Access { } } -impl Access for &mut dyn Access { - unsafe fn command(&mut self, cmd: u8, data: &mut [u8]) -> Result { - (**self).command(cmd, data) - } - - fn data_size(&self) -> usize { - (**self).data_size() - } -} - impl Access for Box { unsafe fn command(&mut self, cmd: u8, data: &mut [u8]) -> Result { (**self).command(cmd, data) @@ -50,3 +41,5 @@ impl Access for Box { (**self).data_size() } } + +downcast_rs::impl_downcast!(Access); diff --git a/tool/src/ec.rs b/tool/src/ec.rs index aed8c77..f22a2f4 100644 --- a/tool/src/ec.rs +++ b/tool/src/ec.rs @@ -245,14 +245,6 @@ impl Ec { self.command(Cmd::LedSetColor, &mut data) } - pub fn as_dyn(&mut self) -> Ec<&mut dyn Access> { - Ec { - access: &mut self.access, - version: self.version, - - } - } - pub fn into_dyn(self) -> Ec> where A: 'static { Ec {