It appears the idea to replace a concrete type by a generic type parameter is naive...
Here I have to fn which only differ by the type used i32 and bool:
https://github.com/hyperledger/indy-sdk/blob/master/libindy/tests/utils/callback.rs#L43
https://github.com/hyperledger/indy-sdk/blob/master/libindy/tests/utils/callback.rs#L95
I chose the bool one and replaced all occurrences of bool by a type parameter T.
Option<extern fn(command_handle: i32, err: ErrorCode,
result: T)>) where T: Sized {
let (sender, receiver) = channel::<(ErrorCode, T)>();
lazy_static! {
static ref CALLBACKS: Mutex<HashMap<i32, Box<FnMut(ErrorCode, T) + Send>>> = Default::default();
}
let closure = Box::new(move |err: ErrorCode, val: T| {
sender.send((err, val)).unwrap();
});
extern "C" fn _callback(command_handle: i32, err: ErrorCode, result: T) {
let mut callbacks = CALLBACKS.lock().unwrap();
let mut cb = callbacks.remove(&command_handle).unwrap();
cb(err, result)
}
let mut callbacks = CALLBACKS.lock().unwrap();
let command_handle = (COMMAND_HANDLE_COUNTER.fetch_add(1, Ordering::SeqCst) + 1) as i32;
callbacks.insert(command_handle, closure);
(receiver, command_handle, Some(_callback))
}
Well that is not so simple. Rust complains
error[E0401]: can't use type parameters from outer function
--> tests/utils/callback.rs:101:75
|
95 | pub fn _closure_to_cb_ec_one<T>() -> (Receiver<(ErrorCode, T)>, i32,
| --------------------- - type variable from outer function
| |
| try adding a local type parameter in this method instead
...
101 | static ref CALLBACKS: Mutex<HashMap<i32, Box<FnMut(ErrorCode, T) + Send>>> = Default::default();
| ^ use of type variable from outer function
error[E0401]: can't use type parameters from outer function
--> tests/utils/callback.rs:108:78
I found several discussion about this but fail to understand how I can fix this.
How do I do this? Thanks.