Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/interface/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@
macro_rules! func {
// Case 1: Generic function — provide function name and types separately
($f:ident :: <$($gen:ty),*>, $fn_type:ty) => {{
let fn_val:$fn_type = $f::<$($gen),*>;
let ptr = fn_val as *const ();
let sig = std::any::type_name_of_val(&fn_val);
let ptr = $f::<$($gen),*> as *const ();
let sig = std::any::type_name::<$fn_type>();
let type_id = std::any::TypeId::of::<$fn_type>();

unsafe { FuncPtr::new_with_type_id(ptr, sig, type_id) }
}};

// Case 2: Non-generic function with explicit type
($f:expr, $fn_type:ty) => {{
let fn_val:$fn_type = $f;
let ptr = fn_val as *const ();
let sig = std::any::type_name_of_val(&fn_val);
let ptr = ($f) as *const ();
let sig = std::any::type_name::<$fn_type>();
let type_id = std::any::TypeId::of::<$fn_type>();

unsafe { FuncPtr::new_with_type_id(ptr, sig, type_id) }
Expand Down
28 changes: 28 additions & 0 deletions tests/will_execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,31 @@ fn test_will_execute_fake_unsafe_unit_assign_and_times_over_called_should_panic(
});
assert!(result.is_err());
}

// Generic function where type parameter S appears inside a slice reference.
// When monomorphized with S = &str, the fn pointer type has different lifetime
// counts than the explicit fn pointer type (issue #107).
fn generic_with_ref_type_param<S: AsRef<str>>(
_prefix: &str,
_items: &[S],
) -> String {
"original".to_string()
}

#[test]
fn test_will_execute_generic_func_with_ref_type_param_via_turbofish() {
let mut injector = InjectorPP::new();
injector
.when_called(injectorpp::func!(
generic_with_ref_type_param::<&str>,
fn(&str, &[&str]) -> String
))
.will_execute(injectorpp::fake!(
func_type: fn(_prefix: &str, _items: &[&str]) -> String,
returns: "mocked".to_string(),
times: 1
));

let result = generic_with_ref_type_param("hello", &["a", "b"]);
assert_eq!(result, "mocked");
}
Loading