-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
const fn
in Trait implementation.
#71971
Comments
Interestingly, I found the #68847 and it has been merged. So now we can have code below compiles: #![feature(const_fn)]
#![feature(const_trait_impl)]
#[derive(Debug, Clone, Copy)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl const std::ops::Add<Vec3> for Vec3 {
type Output = Vec3;
fn add(self, b: Vec3) -> Self::Output {
Vec3 {
x: self.x + b.x,
y: self.y + b.y,
z: self.z + b.z,
}
}
} But that have not directly solves the problem in this issue. Since some trait may contains more than one functions and only some of them are able to be const function. |
@ldm0, rust-lang/rfcs#2632 is still actively being designed. Perhaps you'd like to participate in that discussion? I'll close this since I don't think it's actionable. If the initial implementation is not working as the RFC specifies, feel free to reopen. |
Summary: This allows constructing Bytes or Text statically. Unfortunately traits cannot have const_fn for now: rust-lang/rust#71971 so we manually write out `from_static` for both types. Differential Revision: D39041222 fbshipit-source-id: 9b2312cfd415c1276485a4c2b4a4216db9c02a64
For code like this:
This currently emits:
It would be extremely useful if we could have const fn in trait implementation (especially for operator overloading). And I saw this is also mentioned in the RFC 0911.
The text was updated successfully, but these errors were encountered: