Skip to content
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

x::Atom::none() should be made const fn #210

Closed
ratijas opened this issue Oct 20, 2022 · 4 comments
Closed

x::Atom::none() should be made const fn #210

ratijas opened this issue Oct 20, 2022 · 4 comments

Comments

@ratijas
Copy link

ratijas commented Oct 20, 2022

Use case: I wanted to initialize a global (static) variable with a "none" atom.

@rtbo
Copy link
Collaborator

rtbo commented Oct 22, 2022

Not possible at the moment. Atom::none() is a trait implementation.

impl base::Xid for Atom {
    const fn none() -> Self { Atom { res_id: 0 } }
    fn resource_id(&self) -> u32 { self.res_id }
}
error[E0379]: functions in traits cannot be declared const
    --> /home/remi/dev/rust-xcb/target/debug/build/xcb-a03be5dd4e5ee2d0/out/xproto.rs:5310:5
     |
5310 |     const fn none() -> Self { Atom { res_id: 0 } }
     |     ^^^^^ functions in traits cannot be const

See rust-lang/rust#71971 for tracking.
There is also the const_trait_impl feature which could help.
Don't hesitate to reopen when this becomes stable.

@rtbo rtbo closed this as completed Oct 22, 2022
@ratijas
Copy link
Author

ratijas commented Oct 23, 2022

Is it needed as a trait method? Can every struct/type have a const version of none() which is not a subject to trait method restrictions? Practical code (an app, not a library) dealing with X11 stuff probably won't need that extra level of abstractions anyway.

@ratijas
Copy link
Author

ratijas commented Oct 23, 2022

In fact, I checked out the repo locally, grepped, and haven't found any single trait-usage of none(). But there are, like, only two total.

@rtbo
Copy link
Collaborator

rtbo commented Oct 29, 2022

Is it needed as a trait method?

I'd say no, but I can't change this without breaking semver. I'd need to make a version 2.

I forgot to mention that you can use x::ATOM_NONE for static initialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants