I have an extension trait that adds multiple functions, including a
sqrt() function, to types that implement it. When I implement the trait, I want to defer some function calls such as
sqrt() to the implementation of the underlying primitive, but because I am implementing the function, it has shadowed the the underlying primitive function and I can't call it anymore.
Normally when one trait shadows a function from another type you can do
<my_var as TraitName>::sqrt(), but I can't figure out how to do the equivalent for the
f32::sqrt() function, because it's on the primitive itself.
Typically, the inherent method or function should always take priority over a method/function coming from a trait. Just calling
self.sqrt() in the implementation of your trait should hence still call the standard
f32:sqrt method, not the one from your trait.
Strange, I'm getting the "function cannot return without recursing" error.
Oh, I bet it has something to do with me generating the impl in a macro, and using
$t:ident as a placeholder for either
Messing around to see if I can figure out other options for that.
For example this playground works
I don't know about your concrete macro case. If you need more help, you'd need to provide a code example creating the recursion warning. In the mean time good lock solving it yourself
Thanks. I'm trying to see if I can simplify the problem and if I get a decent example I'll make a playground to demonstrate.
Whoops, I figured it out. The function doesn't exist for the primitive type on
I was confused because the docs for the
core crate link to the
std crate's documentation for primitives, despite not all of the functions existing for the
core versions of the primitives.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.