-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 for SocketAddr::new, SocketAddrV4::new and SocketAddrV6::new #67315
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @shepmaster (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
These functions can't be stable yet, as they use |
make htons const fn This may partially help rust-lang#67315.
make htons const fn This may partially help rust-lang#67315.
make htons const fn This may partially help rust-lang#67315.
…stc_const_unstable attribute
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
make htons const fn This may partially help rust-lang#67315.
r? @oli-obk |
It's worth it to trust but verify what anyone suggests as a fix. In this case, it appears that the attribute should be |
|
make htons const fn This may partially help rust-lang#67315.
make htons const fn This may partially help rust-lang#67315.
Ping from triage - @gliderkite - can you address the build issues in this PR? Thank you. |
@JohnCSimon sorry for the late reply, could you please tell me how can I build and test the changes to make sure it won't fail on CI (without relying on CI to do that if possible)? Is there a doc/reference I can have a look at that states how do do it, before I apply the fix? Thanks. |
It should suffice to run |
I think it would also be good to add the example from your main post as a test file in |
I might have missed something, or proceed in the wrong way, please forgive my inexperience with the subject. When I try to compile with error[E0658]: `match` is not allowed in a `const fn`
--> src/libstd/net/addr.rs:130:9
|
130 | / match ip {
131 | | IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
132 | | IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
133 | | }
| |_________^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable Adding error: `net::addr::SocketAddrV4::new` is not yet stable as a const fn
--> src/libstd/net/addr.rs:131:45
|
131 | IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(const_socket_new)]` to the crate attributes to enable
error: `net::addr::SocketAddrV6::new` is not yet stable as a const fn
--> src/libstd/net/addr.rs:132:45
|
132 | IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(const_socket_new)]` to the crate attributes to enable
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> src/libstd/net/addr.rs:276:28
|
276 | sin_addr: *ip.as_inner(),
| ^^^^^^^^^^^^^
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> src/libstd/net/addr.rs:277:28
|
277 | ..unsafe { mem::zeroed() }
| ^^^^^^^^^^^^^
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> src/libstd/net/addr.rs:369:29
|
369 | sin6_addr: *ip.as_inner(),
| ^^^^^^^^^^^^^
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> src/libstd/net/addr.rs:372:28
|
372 | ..unsafe { mem::zeroed() }
| ^^^^^^^^^^^^^
error: aborting due to 6 previous errors Replacing impl AsInner<c::in6_addr> for Ipv6Addr {
const fn as_inner(&self) -> &c::in6_addr {
&self.inner
}
} leads to the following errors: error[E0658]: references in constant functions may only refer to immutable values
--> src/libcore/mem/maybe_uninit.rs:360:13
|
360 | u.as_mut_ptr().write_bytes(0u8, 1);
| ^ constant functions require immutable values
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> src/libcore/mem/maybe_uninit.rs:360:13
|
360 | u.as_mut_ptr().write_bytes(0u8, 1);
| ^^^^^^^^^^^^^^
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> src/libcore/mem/maybe_uninit.rs:360:13
|
360 | u.as_mut_ptr().write_bytes(0u8, 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors I guess that this is therefore blocked at least on #57349. |
Mutable references could be added by adding the impl AsInner<c::in6_addr> for Ipv6Addr {
const fn as_inner(&self) -> &c::in6_addr {
&self.inner
}
} This isn't possible right now. You can't make trait methods I'm not sure why the |
Ping from triage: @gliderkite - if you think it's appropriate, please close this PR out as S-blocked-closed |
Add
const fn
toSocketAddr::new
,SocketAddrV4::new
andSocketAddrV6::new
to be able to initialize the socket address at compile time, for example:This addition is probably still missing the right method attributes:
as I'm not aware of the procedure required to add const to an existing method in stable.