Skip to content

Commit cfb9e1a

Browse files
committed
Use dyn-clone crate to implement Clone for DeferFn
1 parent d90d7a6 commit cfb9e1a

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clap_builder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ bench = false
5959

6060
[dependencies]
6161
clap_lex = { path = "../clap_lex", version = "0.7.4" }
62+
dyn-clone = "1"
6263
unicase = { version = "2.6.0", optional = true }
6364
strsim = { version = "0.11.0", optional = true }
6465
anstream = { version = "0.6.7", optional = true }

clap_builder/src/builder/command.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,13 @@ use crate::{Error, INTERNAL_ERROR_MSG};
3333
#[cfg(debug_assertions)]
3434
use crate::builder::debug_asserts::assert_app;
3535

36-
// Allows DeferFn to implement Clone but remain object safe
36+
/// Allows [`DeferFn`] to implement [`Clone`]
3737
// see https://stackoverflow.com/a/30353928
38-
trait CloneDynFn: FnOnce(Command) -> Command + Send + Sync {
39-
fn clone_in_box(&self) -> Box<dyn CloneDynFn>;
40-
}
41-
42-
impl<F> CloneDynFn for F
43-
where
44-
F: FnOnce(Command) -> Command + Send + Sync + 'static + Clone,
45-
{
46-
#[inline(always)]
47-
fn clone_in_box(&self) -> Box<dyn CloneDynFn> {
48-
Box::new(self.clone())
49-
}
50-
}
51-
52-
impl Clone for Box<dyn CloneDynFn> {
53-
#[inline(always)]
54-
fn clone(&self) -> Box<dyn CloneDynFn> {
55-
self.clone_in_box()
56-
}
57-
}
38+
trait CloneDynFn: dyn_clone::DynClone + FnOnce(Command) -> Command + Send + Sync {}
39+
impl<F> CloneDynFn for F where F: dyn_clone::DynClone + FnOnce(Command) -> Command + Send + Sync {}
40+
dyn_clone::clone_trait_object!(CloneDynFn);
5841

42+
/// Wraps a function that when called will update a [`Command`] object.
5943
#[derive(Clone)]
6044
struct DeferFn(Box<dyn CloneDynFn>);
6145

@@ -87,7 +71,7 @@ impl DeferFn {
8771
}
8872
}
8973

90-
impl core::fmt::Debug for DeferFn {
74+
impl fmt::Debug for DeferFn {
9175
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9276
f.debug_tuple("DeferFn").field(&"..").finish()
9377
}

0 commit comments

Comments
 (0)