简体   繁体   中英

Simplify function declaration

I would like to simplify the declaration of the following function:

use regex::Regex;

fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn Fn(i32, i32) -> i32 + 'a>)
where F: Fn(i32, i32) -> i32 + 'a
{
    (Regex::new(regex).unwrap(), Box::new(op))
}

I tried to replace the Fn trait by F in the return value, but it raises an error:

fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn F>)
where F: Fn(i32, i32) -> i32 + 'a
{
    (Regex::new(regex).unwrap(), Box::new(op))
}
error[E0404]: expected trait, found type parameter `F`
  --> src/lib.rs:5:55
   |
5  |   fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn F>)
   |                                                         ^ help: a trait with a similar name exists: `Fn`

error: aborting due to previous error

How can simplify this declaration to avoid duplication of Fn(i32, i32) -> i32 + 'a ?

You could use this technique . In short, define a new trait that requires Fn(i32, i32) -> i32 and implement it for any type that already implements Fn(i32, i32) -> i32 :

use regex::Regex;

// 1. Create a new trait
pub trait MyFn: Fn(i32, i32) -> i32 {}

// 2. Implement it
impl<T> MyFn for T where T: Fn(i32, i32) -> i32 {}

fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn MyFn + 'a>)
    where F: MyFn + 'a
{
    (Regex::new(regex).unwrap(), Box::new(op))
}

Note, however, that this might be less readable than just repeating Fn(i32, i32) -> i32 in the signature of oper .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM