Hey folks,
I'm building a library that wraps cross-platform text-to-speech APIs. I'm trying to support callback mechanisms for when utterances begin and end speaking, but each library does things slightly differently and I'm having lots of trouble creating a generic interface. Here, for instance, is how one lower-level library implements its callbacks:
pub fn on_begin(&self, f: Option<fn(u64, u64)>)
In that instance, the second u64 is the ID of the synthesizer. So I can do a lookup in a lazy_static CALLBACKS HashMap keyed off of the ID.
Unfortunately, not all of these libraries pass a client ID. WinRT, for instance, seems like it just passes the tracks that started and stopped playing. Seems like this means I have to arbitrarily assign a unique ID to the synthesizer, but I can't seem to pass variables into closures. I.e. if I do:
let id: u64 = 0; // Assigned elsewhere
sd.0.on_begin(Some(|_msg_id, client_id| {
let callbacks = CALLBACKS.lock().unwrap();
let cb = callbacks.get(&client_id); // works
let callbacks = CALLBACKS.get(&id); // doesn't
}));
Is there any way to get a primitive from outside into a closure? Failing that, is there some other approach I should be using? I know I have a working solution here, but that's only because this particular synth is nice enough to assign and pass its own client IDs, and I don't want to go too far down this rabbit trail if I'm just going to hit a brick wall with the 5 others I need to cover. I think this is the only one that actually assigns an ID to itself, in fact.
Thanks for the help.