-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make core::mem::needs_drop a const fn #54596
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -483,6 +483,16 @@ pub fn align_of_val<T: ?Sized>(val: &T) -> usize { | |
/// ``` | ||
#[inline] | ||
#[stable(feature = "needs_drop", since = "1.21.0")] | ||
#[rustc_const_unstable(feature = "const_needs_drop")] | ||
#[cfg(not(stage0))] | ||
pub const fn needs_drop<T>() -> bool { | ||
intrinsics::needs_drop::<T>() | ||
mjbshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
#[inline] | ||
#[stable(feature = "needs_drop", since = "1.21.0")] | ||
#[cfg(stage0)] | ||
/// Ceci n'est pas la documentation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If #54601 merges first, you can drop this dummy non-const version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, though maybe not with the intrinsic safety change too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the heads up. If #54601 merges first I'll be sure to sync and test with and without the dummy non-const version. |
||
pub fn needs_drop<T>() -> bool { | ||
unsafe { intrinsics::needs_drop::<T>() } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(const_needs_drop)] | ||
|
||
use std::mem; | ||
|
||
struct Trivial(u8, f32); | ||
|
||
struct NonTrivial(u8, String); | ||
|
||
const CONST_U8: bool = mem::needs_drop::<u8>(); | ||
const CONST_STRING: bool = mem::needs_drop::<String>(); | ||
const CONST_TRIVIAL: bool = mem::needs_drop::<Trivial>(); | ||
const CONST_NON_TRIVIAL: bool = mem::needs_drop::<NonTrivial>(); | ||
|
||
static STATIC_U8: bool = mem::needs_drop::<u8>(); | ||
static STATIC_STRING: bool = mem::needs_drop::<String>(); | ||
static STATIC_TRIVIAL: bool = mem::needs_drop::<Trivial>(); | ||
static STATIC_NON_TRIVIAL: bool = mem::needs_drop::<NonTrivial>(); | ||
|
||
fn main() { | ||
assert!(!CONST_U8); | ||
assert!(CONST_STRING); | ||
assert!(!CONST_TRIVIAL); | ||
assert!(CONST_NON_TRIVIAL); | ||
|
||
assert!(!STATIC_U8); | ||
assert!(STATIC_STRING); | ||
assert!(!STATIC_TRIVIAL); | ||
assert!(STATIC_NON_TRIVIAL); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.