How to Call a Method on a Primitive that has Been Shadowed by an Extension Trait

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 f32 or f64.

Messing around to see if I can figure out other options for that.

For example this playground works

Rust Playground

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 :smiley:

1 Like

Thanks. :slight_smile: 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 #![no_std] targets.

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.