I'm trying to write a function that receives a mutable reference to a generic struct from someone else's crate (so I can't modify that struct's definition). Some of the methods implemented for the struct are only available when the struct satisfies a certain bound:
impl<'s, 'p: 's, P> TryCatch<'s, P>
where
Self: AsMut<HandleScope<'p, ()>>,
{
// functions that I want to use
}
My function works fine if I declare it like this:
fn my_func(try_catch: &mut v8::TryCatch<v8::HandleScope>)
or like this:
fn my_func(try_catch: &mut v8::TryCatch<v8::EscapableHandleScope>)
Since there's no function overloading in Rust, I have to either write two functions, write my own trait and then implement it for both flavors of TryCatch
, or come up with some clever way to make the compiler accept either flavor as an argument.
I can pick either of the first two options and it's not a big deal, but I'm trying to learn Rust better, and I was wondering if anyone could help me get the third option working.
PS: Sorry for the clumsy title. If there's a better way to phrase it, let me know.
fn my_func<'s, 'p: 's, P>(try_catch: &mut v8::TryCatch<'s, P>) where v8::TryCatch<'s, P>: AsMut<HandleScope<'p, ()>>
(basically: take the bounds and generics from the function you want to use and copy them on the function where you use it) but it's difficult to be sure because I can't see your error. – trent ᶠᵒʳᵐᵉʳˡʸ ᶜˡ