1
votes

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.

1
It's hard to answer your question because it doesn't contain a minimal reproducible example. Can you elaborate a little, preferably with either a minimized dummy library or the actual library you're relying on? I'm guessing the answer is something like 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 ᶠᵒʳᵐᵉʳˡʸ ᶜˡ
@trentcl That's exactly what I needed! Sorry for the lack of reproducible example, but I was convinced there was something simple that I just didn't grok about Rust. None of the languages I've used before allow me to specify a complex type definition like that on the "left side" of the constraint. Do you think it would be enough to convert your comment to answer so I can accept it, or should I rework the question to make it more reproducible?Vojislav Stojkovic
It's really up to you. If you'd like this Q&A to be preserved for future askers, I'd suggest taking a stab at making a MRE and I'll add an answer when I get some time. But if you don't have time or aren't sure how, that's fine. (But the question will probably be closed without an MRE, whether or not I write a proper answer, so I'm not inclined to at the moment) Either way, glad I could help!trent ᶠᵒʳᵐᵉʳˡʸ ᶜˡ

1 Answers

1
votes

You can achieve what you want also making your function generic. I don't know your particular case, but for instance, both v8::HandleScope and v8::EscapableHandleScope implement the v8::Scoped trait.

So if that's all you need to know about that type you can rewrite your function like:

fn my_func<S>(try_catch: &mut v8::TryCatch<S>)
where
    S: v8::Scoped 

or more shortly:

fn my_func<S: v8::Scoped>(try_catch: &mut v8::TryCatch<S>)