mirror of
https://github.com/rust-lang/rust.git
synced 2025-12-03 14:18:23 +00:00
Constify conversion traits (part 1) This is the first part of rust-lang/rust#144289 being split into smaller pieces. It adds/moves constness of several traits under the `const_convert` feature: * `From` * `Into` * `TryFrom` * `TryInto` * `FromStr` * `AsRef` * `AsMut` * `Borrow` * `BorrowMut` * `Deref` * `DerefMut` There are a few methods that are intrinsically tied to these traits which I've included in the feature. Particularly, those which are wrappers over `AsRef`: * `ByteStr::new` (unstable under `bstr` feature) * `OsStr::new` * `Path::new` Those which directly use `Into`: * `Result::into_ok` * `Result::into_err` And those which use `Deref` and `DerefMut`: * `Pin::as_ref` * `Pin::as_mut` * `Pin::as_deref_mut` * `Option::as_deref` * `Option::as_deref_mut` * `Result::as_deref` * `Result::as_deref_mut` (note: the `Option` and `Result` methods were suggested by ``@npmccallum`` initially as rust-lang/rust#146101) The parts which are missing from this PR are: * Anything that involves heap-allocated types * Making any method const than the ones listed above * Anything that could rely on the above, *or* could rely on system-specific code for `OsStr` or `Path` (note: this mostly makes these methods useless since `str` doesn't implement `AsRef<OsStr>` yet, but it's better to track the method for now and add impls later, IMHO) r? ``@tgross35`` (who mostly already reviewed this)
211 lines
5.5 KiB
Rust
211 lines
5.5 KiB
Rust
// tidy-alphabetical-start
|
|
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
|
|
#![cfg_attr(test, feature(cfg_select))]
|
|
#![feature(alloc_layout_extra)]
|
|
#![feature(array_ptr_get)]
|
|
#![feature(array_try_from_fn)]
|
|
#![feature(array_windows)]
|
|
#![feature(ascii_char)]
|
|
#![feature(ascii_char_variants)]
|
|
#![feature(async_iter_from_iter)]
|
|
#![feature(async_iterator)]
|
|
#![feature(bigint_helper_methods)]
|
|
#![feature(bool_to_result)]
|
|
#![feature(bstr)]
|
|
#![feature(cfg_target_has_reliable_f16_f128)]
|
|
#![feature(char_max_len)]
|
|
#![feature(clone_to_uninit)]
|
|
#![feature(const_convert)]
|
|
#![feature(const_destruct)]
|
|
#![feature(const_eval_select)]
|
|
#![feature(const_ops)]
|
|
#![feature(const_option_ops)]
|
|
#![feature(const_ref_cell)]
|
|
#![feature(const_result_trait_fn)]
|
|
#![feature(const_trait_impl)]
|
|
#![feature(control_flow_ok)]
|
|
#![feature(core_float_math)]
|
|
#![feature(core_intrinsics)]
|
|
#![feature(core_intrinsics_fallbacks)]
|
|
#![feature(core_io_borrowed_buf)]
|
|
#![feature(core_private_bignum)]
|
|
#![feature(core_private_diy_float)]
|
|
#![feature(cstr_display)]
|
|
#![feature(dec2flt)]
|
|
#![feature(drop_guard)]
|
|
#![feature(duration_constants)]
|
|
#![feature(duration_constructors)]
|
|
#![feature(duration_from_nanos_u128)]
|
|
#![feature(error_generic_member_access)]
|
|
#![feature(exact_div)]
|
|
#![feature(exact_size_is_empty)]
|
|
#![feature(extend_one)]
|
|
#![feature(extern_types)]
|
|
#![feature(f16)]
|
|
#![feature(f128)]
|
|
#![feature(float_algebraic)]
|
|
#![feature(float_gamma)]
|
|
#![feature(float_minimum_maximum)]
|
|
#![feature(flt2dec)]
|
|
#![feature(fmt_internals)]
|
|
#![feature(formatting_options)]
|
|
#![feature(freeze)]
|
|
#![feature(future_join)]
|
|
#![feature(generic_assert_internals)]
|
|
#![feature(hasher_prefixfree_extras)]
|
|
#![feature(hashmap_internals)]
|
|
#![feature(int_lowest_highest_one)]
|
|
#![feature(int_roundings)]
|
|
#![feature(ip)]
|
|
#![feature(is_ascii_octdigit)]
|
|
#![feature(isolate_most_least_significant_one)]
|
|
#![feature(iter_advance_by)]
|
|
#![feature(iter_array_chunks)]
|
|
#![feature(iter_collect_into)]
|
|
#![feature(iter_intersperse)]
|
|
#![feature(iter_is_partitioned)]
|
|
#![feature(iter_map_windows)]
|
|
#![feature(iter_next_chunk)]
|
|
#![feature(iter_order_by)]
|
|
#![feature(iter_partition_in_place)]
|
|
#![feature(iterator_try_collect)]
|
|
#![feature(iterator_try_reduce)]
|
|
#![feature(layout_for_ptr)]
|
|
#![feature(lazy_get)]
|
|
#![feature(maybe_uninit_fill)]
|
|
#![feature(maybe_uninit_uninit_array_transpose)]
|
|
#![feature(maybe_uninit_write_slice)]
|
|
#![feature(min_specialization)]
|
|
#![feature(never_type)]
|
|
#![feature(next_index)]
|
|
#![feature(non_exhaustive_omitted_patterns_lint)]
|
|
#![feature(numfmt)]
|
|
#![feature(option_reduce)]
|
|
#![feature(pattern)]
|
|
#![feature(pointer_is_aligned_to)]
|
|
#![feature(portable_simd)]
|
|
#![feature(ptr_metadata)]
|
|
#![feature(result_option_map_or_default)]
|
|
#![feature(slice_from_ptr_range)]
|
|
#![feature(slice_internals)]
|
|
#![feature(slice_partition_dedup)]
|
|
#![feature(slice_split_once)]
|
|
#![feature(split_array)]
|
|
#![feature(split_as_slice)]
|
|
#![feature(std_internals)]
|
|
#![feature(step_trait)]
|
|
#![feature(str_internals)]
|
|
#![feature(strict_provenance_lints)]
|
|
#![feature(test)]
|
|
#![feature(trusted_len)]
|
|
#![feature(trusted_random_access)]
|
|
#![feature(try_blocks)]
|
|
#![feature(try_find)]
|
|
#![feature(try_trait_v2)]
|
|
#![feature(uint_bit_width)]
|
|
#![feature(unsize)]
|
|
#![feature(unwrap_infallible)]
|
|
// tidy-alphabetical-end
|
|
#![allow(internal_features)]
|
|
#![deny(fuzzy_provenance_casts)]
|
|
#![deny(unsafe_op_in_unsafe_fn)]
|
|
|
|
/// Version of `assert_matches` that ignores fancy runtime printing in const context and uses structural equality.
|
|
macro_rules! assert_eq_const_safe {
|
|
($t:ty: $left:expr, $right:expr) => {
|
|
assert_eq_const_safe!($t: $left, $right, concat!(stringify!($left), " == ", stringify!($right)));
|
|
};
|
|
($t:ty: $left:expr, $right:expr$(, $($arg:tt)+)?) => {
|
|
{
|
|
fn runtime() {
|
|
assert_eq!($left, $right, $($($arg)*),*);
|
|
}
|
|
const fn compiletime() {
|
|
const PAT: $t = $right;
|
|
assert!(matches!($left, PAT), $($($arg)*),*);
|
|
}
|
|
core::intrinsics::const_eval_select((), compiletime, runtime)
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Creates a test for runtime and a test for constant-time.
|
|
macro_rules! test_runtime_and_compiletime {
|
|
($(
|
|
$(#[$attr:meta])*
|
|
fn $test:ident() $block:block
|
|
)*) => {
|
|
$(
|
|
$(#[$attr])*
|
|
#[test]
|
|
fn $test() $block
|
|
$(#[$attr])*
|
|
const _: () = $block;
|
|
)*
|
|
}
|
|
}
|
|
|
|
mod alloc;
|
|
mod any;
|
|
mod array;
|
|
mod ascii;
|
|
mod ascii_char;
|
|
mod asserting;
|
|
mod async_iter;
|
|
mod atomic;
|
|
mod bool;
|
|
mod bstr;
|
|
mod cell;
|
|
mod char;
|
|
mod clone;
|
|
mod cmp;
|
|
mod const_ptr;
|
|
mod convert;
|
|
mod ffi;
|
|
mod floats;
|
|
mod fmt;
|
|
mod future;
|
|
mod hash;
|
|
mod hint;
|
|
mod intrinsics;
|
|
mod io;
|
|
mod iter;
|
|
mod lazy;
|
|
mod macros;
|
|
mod manually_drop;
|
|
mod mem;
|
|
mod net;
|
|
mod nonzero;
|
|
mod num;
|
|
mod ops;
|
|
mod option;
|
|
mod panic;
|
|
mod pattern;
|
|
mod pin;
|
|
mod pin_macro;
|
|
mod ptr;
|
|
mod result;
|
|
mod simd;
|
|
mod slice;
|
|
mod str;
|
|
mod str_lossy;
|
|
mod task;
|
|
mod time;
|
|
mod tuple;
|
|
mod unicode;
|
|
mod waker;
|
|
mod wtf8;
|
|
|
|
/// Copied from `std::test_helpers::test_rng`, see that function for rationale.
|
|
#[track_caller]
|
|
#[allow(dead_code)] // Not used in all configurations.
|
|
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
|
use core::hash::{BuildHasher, Hash, Hasher};
|
|
let mut hasher = std::hash::RandomState::new().build_hasher();
|
|
core::panic::Location::caller().hash(&mut hasher);
|
|
let hc64 = hasher.finish();
|
|
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
|
|
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
|
|
rand::SeedableRng::from_seed(seed)
|
|
}
|