How to make rustc understand that a reference to a function pointer can have a longer lifetime than the parameter/return types of the function it points to

⚓ Rust    📅 2025-07-01    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 36 days ago. The information described in this article may have changed.
struct Criterion {
  // ...
}
// This trait definition will not compile unless I add a 'static bound to it,
// apparently because it doesn't want a &'static reference to a type that has
// a non-'static generic parameter (the argument to the function).
// This is an unacceptable limitation for my use case.
// Is there some workaround I can use?
trait SomeTrait {
   const CASES: &[(Criterion, fn(Self) -> ReturnType)];
}

fn transform<T: SomeTrait>(value: T, request: String) -> ReturnType {
    for (criterion, transform_func) in T::CASES {
        if criterion.matches(&request) {
            return (transform_func)(value);
        }
    }
    ReturnType::default()
}

4 posts - 2 participants

Read full topic

🏷️ rust_feed