Skip to content

Commit

Permalink
chore: where possible replace async-trait with native async trait s…
Browse files Browse the repository at this point in the history
…upport in 1.75+ (reubeno#197)

Reduce usage of the async-trait crate where possible, using the async_fn_in_trait native feature in 1.75+.
  • Loading branch information
39555 committed Oct 22, 2024
1 parent b4a7933 commit 48c0188
Show file tree
Hide file tree
Showing 50 changed files with 123 additions and 167 deletions.
19 changes: 10 additions & 9 deletions brush-core/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub type CommandExecuteFunc = fn(
pub type CommandContentFunc = fn(&str, ContentType) -> Result<String, error::Error>;

/// Trait implemented by built-in shell commands.
#[async_trait::async_trait]
pub trait Command: Parser {
/// Instantiates the built-in command with the given arguments.
///
Expand Down Expand Up @@ -208,10 +208,11 @@ pub trait Command: Parser {
/// # Arguments
///
/// * `context` - The context in which the command is being executed.
async fn execute(
// NOTE: we use desugared async here because we need a Send marker
fn execute(
&self,
context: commands::ExecutionContext<'_>,
) -> Result<ExitCode, error::Error>;
) -> impl std::future::Future<Output = Result<ExitCode, error::Error>> + std::marker::Send;

/// Returns the textual help content associated with the command.
///
Expand All @@ -236,7 +237,7 @@ pub trait Command: Parser {

/// Trait implemented by built-in shell commands that take specially handled declarations
/// as arguments.
#[async_trait::async_trait]
pub trait DeclarationCommand: Command {
/// Stores the declarations within the command instance.
///
Expand Down Expand Up @@ -395,8 +396,7 @@ fn brush_help_styles() -> clap::builder::Styles {
/// # Returns
///
/// * a parsed struct T from [`clap::Parser::parse_from`]
/// * the remain iterator `args` with `--` and the rest arguments if they present
/// othervise None
/// * the remain iterator `args` with `--` and the rest arguments if they present othervise None
///
/// # Examples
/// ```
Expand All @@ -420,8 +420,8 @@ where
S: Into<std::ffi::OsString> + Clone + PartialEq<&'static str>,
{
let mut args = args.into_iter();
// the best way to save `--` is to get it out with a side effect while `clap` iterates over the args
// this way we can be 100% sure that we have '--' and the remaining args
// the best way to save `--` is to get it out with a side effect while `clap` iterates over the
// args this way we can be 100% sure that we have '--' and the remaining args
// and we will iterate only once
let mut hyphen = None;
let args_before_hyphen = args.by_ref().take_while(|a| {
Expand All @@ -437,7 +437,8 @@ where
}

/// Similar to [`parse_known`] but with [`clap::Parser::try_parse_from`]
/// This function is used to parse arguments in builtins such as [`crate::builtins::echo::EchoCommand`]
/// This function is used to parse arguments in builtins such as
/// [`crate::builtins::echo::EchoCommand`]
pub fn try_parse_known<T: Parser>(
args: impl IntoIterator<Item = String>,
) -> Result<(T, Option<impl Iterator<Item = String>>), clap::Error> {
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) struct AliasCommand {
aliases: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for AliasCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/bg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub(crate) struct BgCommand {
job_specs: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for BgCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/break_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub(crate) struct BreakCommand {
which_loop: i8,
}

#[async_trait::async_trait]
impl builtins::Command for BreakCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/brushinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ enum CompleteCommand {
},
}

#[async_trait::async_trait]
impl builtins::Command for BrushInfoCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/builtin_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub(crate) struct BuiltinCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for BuiltinCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub(crate) struct CdCommand {
target_dir: Option<PathBuf>,
}

#[async_trait::async_trait]
impl builtins::Command for CdCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/colon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub(crate) struct ColonCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for ColonCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub(crate) struct CommandCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for CommandCommand {
async fn execute(
&self,
Expand Down
3 changes: 0 additions & 3 deletions brush-core/src/builtins/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ pub(crate) struct CompleteCommand {
names: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for CompleteCommand {
async fn execute(
&self,
Expand Down Expand Up @@ -435,7 +434,6 @@ pub(crate) struct CompGenCommand {
word: Option<String>,
}

#[async_trait::async_trait]
impl builtins::Command for CompGenCommand {
async fn execute(
&self,
Expand Down Expand Up @@ -514,7 +512,6 @@ pub(crate) struct CompOptCommand {
names: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for CompOptCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/continue_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub(crate) struct ContinueCommand {
which_loop: i8,
}

#[async_trait::async_trait]
impl builtins::Command for ContinueCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl builtins::DeclarationCommand for DeclareCommand {
}

#[allow(clippy::too_many_lines)]
#[async_trait::async_trait]
impl builtins::Command for DeclareCommand {
fn takes_plus_options() -> bool {
true
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub(crate) struct DirsCommand {
// TODO: implement +N and -N
}

#[async_trait::async_trait]
impl builtins::Command for DirsCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) struct DotCommand {
pub script_args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for DotCommand {
async fn execute(
&self,
Expand Down
7 changes: 3 additions & 4 deletions brush-core/src/builtins/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ pub(crate) struct EchoCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for EchoCommand {
/// Override the default [builtins::Command::new] function to handle clap's limitation related to `--`.
/// See [crate::builtins::parse_known] for more information
/// Override the default [`builtins::Command::new`] function to handle clap's limitation related
/// to `--`. See [`builtins::parse_known`] for more information
/// TODO: we can safely remove this after the issue is resolved
fn new<I>(args: I) -> Result<Self, clap::Error>
where
Expand Down Expand Up @@ -75,6 +74,6 @@ impl builtins::Command for EchoCommand {
write!(context.stdout(), "{s}")?;
context.stdout().flush()?;

return Ok(builtins::ExitCode::Success);
Ok(builtins::ExitCode::Success)
}
}
1 change: 0 additions & 1 deletion brush-core/src/builtins/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub(crate) struct EnableCommand {
names: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for EnableCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) struct EvalCommand {
pub args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for EvalCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub(crate) struct ExecCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for ExecCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) struct ExitCommand {
code: Option<i32>,
}

#[async_trait::async_trait]
impl builtins::Command for ExitCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl builtins::DeclarationCommand for ExportCommand {
}
}

#[async_trait::async_trait]
impl builtins::Command for ExportCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/false_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{builtins, commands};
#[derive(Parser)]
pub(crate) struct FalseCommand {}

#[async_trait::async_trait]
impl builtins::Command for FalseCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/fg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub(crate) struct FgCommand {
job_spec: Option<String>,
}

#[async_trait::async_trait]
impl builtins::Command for FgCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub(crate) struct GetOptsCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for GetOptsCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub(crate) struct HelpCommand {
topic_patterns: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for HelpCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub(crate) struct JobsCommand {
job_specs: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for JobsCommand {
async fn execute(
&self,
Expand Down
4 changes: 1 addition & 3 deletions brush-core/src/builtins/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub(crate) struct KillCommand {

//
// TODO: implement -sigspec syntax
//
/// List known signal names.
#[arg(short = 'l', short_alias = 'L')]
list_signals: bool,
Expand All @@ -25,7 +24,6 @@ pub(crate) struct KillCommand {
args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for KillCommand {
async fn execute(
&self,
Expand All @@ -39,7 +37,7 @@ impl builtins::Command for KillCommand {
}

if self.list_signals {
return error::unimp("kill -l");
error::unimp("kill -l")
} else {
if self.args.len() != 1 {
writeln!(context.stderr(), "{}: invalid usage", context.command_name)?;
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/let_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub(crate) struct LetCommand {
exprs: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for LetCommand {
async fn execute(
&self,
Expand Down
2 changes: 0 additions & 2 deletions brush-core/src/builtins/popd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ pub(crate) struct PopdCommand {
no_directory_change: bool,
//
// TODO: implement +N and -N
//
}

#[async_trait::async_trait]
impl builtins::Command for PopdCommand {
async fn execute(
&self,
Expand Down
3 changes: 1 addition & 2 deletions brush-core/src/builtins/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub(crate) struct PrintfCommand {
format_and_args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for PrintfCommand {
async fn execute(
&self,
Expand All @@ -31,7 +30,7 @@ impl builtins::Command for PrintfCommand {
context.stdout().flush()?;
}

return Ok(builtins::ExitCode::Success);
Ok(builtins::ExitCode::Success)
}
}

Expand Down
2 changes: 0 additions & 2 deletions brush-core/src/builtins/pushd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ pub(crate) struct PushdCommand {
dir: String,
//
// TODO: implement +N and -N
//
}

#[async_trait::async_trait]
impl builtins::Command for PushdCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/pwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub(crate) struct PwdCommand {
allow_symlinks: bool,
}

#[async_trait::async_trait]
impl builtins::Command for PwdCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/return_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) struct ReturnCommand {
code: Option<i32>,
}

#[async_trait::async_trait]
impl builtins::Command for ReturnCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub(crate) struct SetCommand {
positional_args: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for SetCommand {
fn takes_plus_options() -> bool {
true
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) struct ShiftCommand {
n: Option<i32>,
}

#[async_trait::async_trait]
impl builtins::Command for ShiftCommand {
async fn execute(
&self,
Expand Down
1 change: 0 additions & 1 deletion brush-core/src/builtins/shopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub(crate) struct ShoptCommand {
options: Vec<String>,
}

#[async_trait::async_trait]
impl builtins::Command for ShoptCommand {
async fn execute(
&self,
Expand Down
Loading

0 comments on commit 48c0188

Please sign in to comment.