#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![cfg_attr(test, allow(clippy::non_ascii_literal))]
#![cfg_attr(test, allow(clippy::shadow_unrelated))]
#![allow(unknown_lints)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_alias))]
#![no_std]
#![doc(html_root_url = "https://docs.rs/qed/1.6.1")]
#[cfg(all(doctest, any(target_pointer_width = "32", target_pointer_width = "64")))]
#[doc = include_str!("../README.md")]
mod readme {}
#[doc(hidden)]
#[allow(missing_docs)]
pub mod imp;
#[macro_export]
macro_rules! const_assert {
($x:expr $(,)?) => {
const _: () = ::core::assert!($x);
};
}
#[macro_export]
macro_rules! const_assert_eq {
($x:expr, $y:expr $(,)?) => {
$crate::const_assert!($x == $y);
};
}
#[macro_export]
macro_rules! const_assert_ne {
($x:expr, $y:expr $(,)?) => {
$crate::const_assert!($x != $y);
};
}
#[macro_export]
macro_rules! const_assert_matches {
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
$crate::const_assert!(match $left {
$( $pattern )|+ $( if $guard )? => true,
_ => false,
});
};
}
#[macro_export]
macro_rules! lossless_cast_u32_to_usize {
($num:expr) => {{
$crate::const_assert!(::core::primitive::usize::BITS >= ::core::primitive::u32::BITS);
let num: ::core::primitive::u32 = $num;
num as ::core::primitive::usize
}};
}
#[macro_export]
macro_rules! const_assert_size_eq {
($left:ty, $right:ty $(,)?) => {
const _: () = {
let _assert = ::core::mem::transmute::<$left, $right>;
};
};
}
#[macro_export]
macro_rules! const_assert_bytes_has_no_nul {
($bytes:expr $(,)?) => {{
const _: &[::core::primitive::u8] = $bytes;
$crate::const_assert!(!$crate::imp::contains_nul($bytes));
}};
}
#[macro_export]
macro_rules! const_cstr_from_bytes {
($bytes:expr $(,)?) => {{
const _: &[::core::primitive::u8] = $bytes;
$crate::const_assert!($crate::imp::is_cstr($bytes));
unsafe { ::core::ffi::CStr::from_bytes_with_nul_unchecked($bytes) }
}};
}
#[macro_export]
macro_rules! const_cstr_from_str {
($str:expr $(,)?) => {{
const _: &::core::primitive::str = $str;
$crate::const_cstr_from_bytes!($str.as_bytes())
}};
}
#[cfg(test)]
mod tests {
use ::core::ffi::CStr;
use ::core::num::NonZeroU8;
mod core {}
mod std {}
mod imp {}
#[test]
fn const_assert_no_warnings() {
crate::const_assert!(true);
crate::const_assert!(NonZeroU8::new(0).is_none());
crate::const_assert!(NonZeroU8::new(29).is_some());
}
#[test]
fn const_assert_eq_no_warnings() {
crate::const_assert_eq!(i8::BITS, u8::BITS);
crate::const_assert_eq!(u8::BITS, u8::BITS);
}
#[test]
fn const_assert_eq_no_warnings_literals() {
crate::const_assert_eq!(0, 0);
crate::const_assert_eq!(29_i32, 29_i32);
}
#[test]
fn const_assert_ne_no_warnings() {
crate::const_assert_ne!(u32::BITS, u8::BITS);
}
#[test]
fn const_assert_ne_no_warings_literals() {
crate::const_assert_ne!(9, 99);
crate::const_assert_ne!(0_i32, 29_i32);
}
#[test]
fn const_assert_matches_no_warnings() {
crate::const_assert_matches!(NonZeroU8::new(0), None);
crate::const_assert_matches!(NonZeroU8::new(29), Some(_));
crate::const_assert_matches!(NonZeroU8::new(29), Some(x) if x.get() == 29);
}
#[test]
fn const_assert_matches_no_warnings_literals() {
crate::const_assert_matches!(None::<i8>, None);
crate::const_assert_matches!(Some(0_i8), Some(_));
crate::const_assert_matches!(None::<i8>, None::<i8>);
crate::const_assert_matches!(Some(0_i8), Some(0_i8));
}
#[test]
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
fn lossless_cast_u32_usize_no_warnings() {
let n = crate::lossless_cast_u32_to_usize!(29_u32);
assert_eq!(n, 29_usize);
}
#[test]
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
fn lossless_cast_u32_usize_no_warnings_const() {
const N: usize = crate::lossless_cast_u32_to_usize!(29_u32);
assert_eq!(N, 29_usize);
}
#[test]
fn size_eq_reference_transmute_no_warnings() {
crate::const_assert_size_eq!(&[u8], &str);
}
#[test]
fn size_eq_pointer_transmute_no_warnings() {
crate::const_assert_size_eq!(*mut u8, *const u8);
}
#[test]
fn const_assert_bytes_has_no_nul_no_warnings() {
crate::const_assert_bytes_has_no_nul!("abcdefg".as_bytes());
crate::const_assert_bytes_has_no_nul!("".as_bytes());
}
#[test]
fn const_cstr_from_bytes_no_warnings() {
const CSTR: &CStr = crate::const_cstr_from_bytes!("Array\0".as_bytes());
const EMPTY: &CStr = crate::const_cstr_from_bytes!("\0".as_bytes());
assert_eq!(CSTR.to_bytes(), b"Array");
assert!(EMPTY.to_bytes().is_empty());
}
#[test]
fn const_cstr_from_str_no_warnings() {
const CSTR: &CStr = crate::const_cstr_from_str!("Array\0");
const EMPTY: &CStr = crate::const_cstr_from_str!("\0");
assert_eq!(CSTR.to_bytes(), b"Array");
assert!(EMPTY.to_bytes().is_empty());
}
#[test]
fn const_assert_bytes_has_no_nul_none_shadow() {
#[allow(dead_code)]
#[allow(non_upper_case_globals)]
const None: () = ();
crate::const_assert_bytes_has_no_nul!("abcdefg".as_bytes());
}
#[test]
fn const_assert_bytes_has_no_nul_hygiene() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct u8 {}
crate::const_assert_bytes_has_no_nul!("abcdefg".as_bytes());
}
#[test]
fn const_assert_hygiene_assert() {
#[allow(unused_macros)]
macro_rules! assert {
($e:expr) => {
panic!("::core::assert! was shadowed")
};
}
crate::const_assert!("".is_empty());
}
#[test]
fn const_assert_hygiene_bool() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct bool {}
crate::const_assert!("".is_empty());
}
#[test]
fn const_assert_hygiene_usize() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct usize {}
crate::const_assert!("".is_empty());
}
#[test]
fn lossless_u32_to_usize_hygiene_u32() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct u32 {}
let n = crate::lossless_cast_u32_to_usize!(29_u32);
assert_eq!(n, 29_usize);
}
#[test]
fn lossless_u32_to_usize_hygiene_usize() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct usize {}
let n = crate::lossless_cast_u32_to_usize!(29_u32);
assert_eq!(n, 29_usize);
}
#[test]
fn const_cstr_from_bytes_hygiene() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct u8 {}
const _: &CStr = crate::const_cstr_from_str!("Abc\0");
}
#[test]
fn const_cstr_from_str_hygiene_str() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct str {}
const _: &CStr = crate::const_cstr_from_str!("Abc\0");
}
#[test]
fn const_cstr_from_str_hygiene_u8() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct u8 {}
const _: &CStr = crate::const_cstr_from_str!("Abc\0");
}
#[test]
fn const_cstr_from_str_hygiene_str_u8() {
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct str {}
#[allow(dead_code)]
#[allow(non_camel_case_types)]
struct u8 {}
const _: &CStr = crate::const_cstr_from_str!("Abc\0");
}
}