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

Make Vec::len() and Vec::is_empty() const fn #66462

Closed
wants to merge 2 commits into from

Conversation

GrayJack
Copy link
Contributor

@GrayJack GrayJack commented Nov 16, 2019

Making these two functions const fn will improve the usability in constant context.

This also open the possibility of contify String functions with the same names.

Maybe this is putting the cart before the horse, since we don't have const allocation yet and should be delayed till we have one

@rust-highfive
Copy link
Contributor

r? @Mark-Simulacrum

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 16, 2019
@Mark-Simulacrum
Copy link
Member

r? @oli-obk

@memoryruins
Copy link
Contributor

#65341 aimed to add these last month, but it was closed due to
not having a clear gain for usability in const fn at this time.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 16, 2019

Yea, we're going to delay making these const fn until someone has a use case. That said, I wonder if we can coerce a vec to a slice in const eval and call slice's len method

@GrayJack
Copy link
Contributor Author

Can't we use once_cell Lazy to have a initialized Vec constant?
Like this

const a: Lazy<Vec<i8>> = Lazy::new(|| vec![1, 2, 3]);

@memoryruins
Copy link
Contributor

vec!, due to allocations, and fn pointers cannot be called directly in const contexts. Lazy obtains a fn pointer fn() -> Vec<i8> from the closure, which is called for initialization at first use during runtime. (To initialize only once, still at runtime, and at a specific memory location, Lazy is used in statics).

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 30, 2019
@KodrAus KodrAus mentioned this pull request Nov 30, 2019
6 tasks
@JohnCSimon
Copy link
Member

Ping from triage
@oli-obk - can you please review this PR?

@oli-obk
Copy link
Contributor

oli-obk commented Dec 7, 2019

Closing for now. If you come up with a use case where these are needed, please create an issue for it so it can be discussed independently of an implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants