Skip to content

Commit e9ea611

Browse files
committed
fix(test2)!: Dont require boxing in FnCase
1 parent 4f8f05f commit e9ea611

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

crates/libtest2/examples/tidy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use libtest2::Case;
12
use libtest2::FnCase;
23
use libtest2::RunError;
34
use libtest2::RunResult;
@@ -23,8 +24,8 @@ fn main() -> std::io::Result<()> {
2324

2425
/// Creates one test for each `.rs` file in the current directory or
2526
/// sub-directories of the current directory.
26-
fn collect_tests() -> std::io::Result<Vec<FnCase>> {
27-
fn visit_dir(path: &std::path::Path, tests: &mut Vec<FnCase>) -> std::io::Result<()> {
27+
fn collect_tests() -> std::io::Result<Vec<Box<dyn Case>>> {
28+
fn visit_dir(path: &std::path::Path, tests: &mut Vec<Box<dyn Case>>) -> std::io::Result<()> {
2829
let current_dir = std::env::current_dir()?;
2930
for entry in std::fs::read_dir(path)? {
3031
let entry = entry?;
@@ -45,7 +46,7 @@ fn collect_tests() -> std::io::Result<Vec<FnCase>> {
4546
.into_owned();
4647

4748
let test = FnCase::test(name, move |_| check_file(&path));
48-
tests.push(test);
49+
tests.push(Box::new(test));
4950
}
5051
} else if file_type.is_dir() {
5152
// Handle directories

crates/libtest2/src/case.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,27 @@ impl Case for DynCase {
2727
}
2828
}
2929

30-
pub struct FnCase {
30+
pub struct FnCase<R> {
3131
name: String,
32-
#[allow(clippy::type_complexity)]
33-
runner: Box<dyn Fn(&TestContext) -> RunResult + Send + Sync>,
32+
runner: R,
3433
}
3534

36-
impl FnCase {
37-
pub fn test(
38-
name: impl Into<String>,
39-
runner: impl Fn(&TestContext) -> RunResult + Send + Sync + 'static,
40-
) -> Self {
35+
impl<R> FnCase<R>
36+
where
37+
R: Fn(&TestContext) -> RunResult + Send + Sync + 'static,
38+
{
39+
pub fn test(name: impl Into<String>, runner: R) -> Self {
4140
Self {
4241
name: name.into(),
43-
runner: Box::new(runner),
42+
runner,
4443
}
4544
}
4645
}
4746

48-
impl Case for FnCase {
47+
impl<R> Case for FnCase<R>
48+
where
49+
R: Fn(&TestContext) -> RunResult + Send + Sync + 'static,
50+
{
4951
fn name(&self) -> &str {
5052
&self.name
5153
}

crates/libtest2/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ pub mod panic;
5959
pub mod _private {
6060
pub use distributed_list::push;
6161
pub use distributed_list::DistributedList;
62-
pub use libtest2_harness::Case;
6362
pub use libtest2_harness::Source;
6463
pub use libtest2_harness::TestKind;
6564

@@ -72,6 +71,7 @@ pub mod _private {
7271

7372
pub use case::main;
7473
pub use case::FnCase;
74+
pub use libtest2_harness::Case;
7575
pub use libtest2_harness::IntoRunResult;
7676
pub use libtest2_harness::RunError;
7777
pub use libtest2_harness::RunResult;

crates/libtest2/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ macro_rules! _test_parse {
106106
#[allow(non_camel_case_types)]
107107
struct $name;
108108

109-
impl $crate::_private::Case for $name {
109+
impl $crate::Case for $name {
110110
fn name(&self) -> &str {
111111
$crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name));
112112

0 commit comments

Comments
 (0)