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 Bytes::from_static a const fn? #311

Merged
merged 1 commit into from Nov 20, 2019

Conversation

andrewtj
Copy link
Contributor

@andrewtj andrewtj commented Nov 9, 2019

I think it'd be useful to make Bytes::from_static a const fn so that Bytes and types built on Bytes can be used in consts and statics. This would require a minimum Rust version of 1.39 though. (Opened a PR to discuss this instead of an issue because it's a two line change.)

@andrewtj
Copy link
Contributor Author

andrewtj commented Nov 9, 2019

Oops, should have run the test suite. It's not as small a change as I thought:

error[E0723]: can only call other `const fn` within a `const fn`, but `const loom::sync::atomic::ptr::AtomicPtr::<()>::new` is not stable as `const fn`
   --> tests/../src/bytes.rs:121:19
    |
121 |             data: AtomicPtr::new(ptr::null_mut()),
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
    = help: add `#![feature(const_fn)]` to the crate attributes to enable

error[E0716]: temporary value dropped while borrowed
   --> tests/../src/bytes.rs:122:22
    |
122 |             vtable: &STATIC_VTABLE,
    |                     -^^^^^^^^^^^^^
    |                     ||
    |                     |creates a temporary which is freed while still in use
    |                     requires that borrow lasts for `'static`
123 |         }
124 |     }
    |     - temporary value is freed at the end of this statement

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0716, E0723.
For more information about an error, try `rustc --explain E0716`.
error: could not compile `bytes`.
warning: build failed, waiting for other jobs to finish...
error: build failed

@carllerche
Copy link
Member

Maybe there is a way to scope out loom?

@seanmonstar
Copy link
Member

If we were to switch the loom support to use cfg(loom), it'd be easier to fix this.

@carllerche
Copy link
Member

@seanmonstar that is what we are moving to in Tokio due to edge cases. We probably will want to do that here as well.

@seanmonstar
Copy link
Member

Rebasing on master could allow you to only make this a const fn when cfg(not(all(loom, test))).

Rust versions bumped to the minimum needed for const slice len.
@andrewtj
Copy link
Contributor Author

Thanks. I've made that change and also bumped Rust versions.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@carllerche carllerche merged commit 59aea9e into tokio-rs:master Nov 20, 2019
@andrewtj andrewtj deleted the const-bytes branch November 21, 2019 21:40
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

Successfully merging this pull request may close these issues.

None yet

3 participants