use core::fmt::{Debug, Display, Write as FmtWrite};
use core::future::{Future, IntoFuture};
use core::hash::Hash;
use core::ops::Deref;
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::str::FromStr;
#[cfg(feature = "std")]
use std::io::{Read as IoRead, Write as IoWrite};
use crate::LifetimeFree;
#[macro_export]
macro_rules! define_impls_trait_ignore_lt_fn {
( #[auto_doc] $( #[$meta:meta] )* $vis:vis $fn_name:ident: $( $bounds:tt )+ ) => {
define_impls_trait_ignore_lt_fn! {
#[doc = "Returns `true` if the given type implements `"]
#[doc = stringify!( $( $bounds )+ )]
#[doc = "`."]
#[+reliability_doc]
$( #[$meta] )*
$vis $fn_name: $( $bounds )+
}
};
(
$( #[$meta1:meta] )* #[+reliability_doc] $( #[$meta2:meta] )*
$vis:vis $fn_name:ident: $( $bounds:tt )+
) => {
define_impls_trait_ignore_lt_fn! {
$( #[$meta1] )*
$( #[$meta2] )*
$vis $fn_name: $( $bounds )+
}
};
( $( #[$meta:meta] )* $vis:vis $fn_name:ident: $( $bounds:tt )+ ) => {
$( #[$meta] )*
#[inline]
#[must_use]
$vis fn $fn_name<T>() -> bool
where
T: ?Sized
{
struct Impl<'a, T>(&'a ::core::cell::Cell<bool>, ::core::marker::PhantomData<T>)
where
T: ?Sized;
impl<T> PartialEq for Impl<'_, T>
where
T: ?Sized,
{
fn eq(&self, _other: &Self) -> bool {
let _ = self.0.set(true);
true
}
}
impl<T> Eq for Impl<'_, T> where T: ?Sized + $( $bounds )+ {}
let not_impls_trait = ::core::cell::Cell::new(false);
let rc = $crate::macro_deps::Rc::new(Impl(
¬_impls_trait,
::core::marker::PhantomData::<T>
));
let _ = rc == rc;
!not_impls_trait.get()
}
};
}
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_sized_weak: Sized);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_send_weak: Send);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_sync_weak: Sync);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_unpin_weak: Unpin);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_unwind_safe_weak: UnwindSafe);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_ref_unwind_safe_weak: RefUnwindSafe);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_deref_weak: Deref);
define_impls_trait_ignore_lt_fn!(
#[auto_doc]
pub impls_copy_weak: Copy
);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_clone_weak: Clone);
define_impls_trait_ignore_lt_fn!(
#[auto_doc]
pub impls_eq_weak: Eq
);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_partial_eq_weak: PartialEq);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_ord_weak: Ord);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_partial_ord_weak: PartialOrd);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_hash_weak: Hash);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_default_weak: Default);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_debug_weak: Debug);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_display_weak: Display);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_from_str_weak: FromStr);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_iterator_weak: Iterator);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_into_iterator_weak: IntoIterator);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_future_weak: Future);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_into_future_weak: IntoFuture);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_fmt_write_weak: FmtWrite);
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_io_read_weak: IoRead);
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_io_write_weak: IoWrite);
define_impls_trait_ignore_lt_fn!(#[auto_doc] pub impls_lifetime_free_weak: LifetimeFree);
#[cfg(test)]
mod tests {
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use crate::unreliable::{
impls_clone_weak, impls_copy_weak, impls_eq_weak, impls_lifetime_free_weak,
impls_partial_eq_weak,
};
#[test]
fn test_impls_copy() {
#[derive(Copy, Clone)]
struct Copiable;
#[derive(Clone)]
struct Cloneable;
struct NonCloneable;
assert!(impls_copy_weak::<()>());
assert!(impls_copy_weak::<u32>());
assert!(impls_copy_weak::<f64>());
assert!(impls_copy_weak::<Copiable>());
assert!(!impls_copy_weak::<Cloneable>());
assert!(!impls_copy_weak::<NonCloneable>());
}
#[cfg(feature = "alloc")]
#[test]
fn test_impls_copy_alloc() {
assert!(impls_copy_weak::<&String>());
assert!(impls_copy_weak::<&Vec<u8>>());
assert!(!impls_copy_weak::<String>());
assert!(!impls_copy_weak::<Vec<u8>>());
assert!(!impls_copy_weak::<&mut String>());
assert!(!impls_copy_weak::<&mut Vec<u8>>());
}
#[test]
fn test_impls_clone() {
#[derive(Copy, Clone)]
struct Copiable;
#[derive(Clone)]
struct Cloneable;
struct NonCloneable;
assert!(impls_clone_weak::<()>());
assert!(impls_clone_weak::<u32>());
assert!(impls_clone_weak::<f64>());
assert!(impls_clone_weak::<Copiable>());
assert!(impls_clone_weak::<Cloneable>());
assert!(!impls_clone_weak::<NonCloneable>());
}
#[test]
fn test_impls_eq() {
assert!(impls_eq_weak::<()>());
assert!(impls_eq_weak::<u32>());
assert!(!impls_eq_weak::<f64>());
}
#[cfg(feature = "alloc")]
#[test]
fn test_impls_eq_alloc() {
assert!(impls_eq_weak::<&String>());
assert!(impls_eq_weak::<&Vec<u8>>());
assert!(impls_eq_weak::<String>());
assert!(impls_eq_weak::<Vec<u8>>());
assert!(impls_eq_weak::<&mut String>());
assert!(impls_eq_weak::<&mut Vec<u8>>());
}
#[test]
fn test_impls_partial_eq() {
assert!(impls_partial_eq_weak::<()>());
assert!(impls_partial_eq_weak::<u32>());
assert!(impls_partial_eq_weak::<f64>());
}
#[cfg(feature = "alloc")]
#[test]
fn test_impls_partial_eq_alloc() {
assert!(impls_partial_eq_weak::<&String>());
assert!(impls_partial_eq_weak::<&Vec<u8>>());
assert!(impls_partial_eq_weak::<String>());
assert!(impls_partial_eq_weak::<Vec<u8>>());
assert!(impls_partial_eq_weak::<&mut String>());
assert!(impls_partial_eq_weak::<&mut Vec<u8>>());
}
#[test]
fn test_lifetime_free() {
assert!(impls_lifetime_free_weak::<()>());
assert!(impls_lifetime_free_weak::<u32>());
assert!(impls_lifetime_free_weak::<f64>());
}
#[cfg(feature = "alloc")]
#[test]
fn test_lifetime_free_alloc() {
assert!(!impls_lifetime_free_weak::<&String>());
assert!(!impls_lifetime_free_weak::<&Vec<u8>>());
assert!(impls_lifetime_free_weak::<String>());
assert!(impls_lifetime_free_weak::<Vec<u8>>());
assert!(!impls_lifetime_free_weak::<&mut String>());
assert!(!impls_lifetime_free_weak::<&mut Vec<u8>>());
}
}