Struct TypeHelper

Source
pub struct TypeHelper<T: ?Sized>(/* private fields */);
Expand description

A helper struct used to determine whether a type implements traits like Send, Sync, and Clone by cooperating with helper traits like NotSend , NotSync, and NotClone.

Helper traits basically have associated constants meaning whether a type imeplements a trait such as Send. They are set to false by default, and all types implement the helper types by blanket implementation. In other words, all types are not Send, Sync by the helper traits, etc. But TypeHelper can overwrite it for types that actually implement those traits thanks to its trait bound. As a result, clients can be aware of whether a type impelments a certain trait through TypeHelper. This is especially useful when you make a library that receives anonymous type and you need such type information.

§How it works

If a struct has function foo<T: Send> and it also implement Foo which has the same signature function foo<T>, then rust will look for callable function in the order below.

  • Inherent function
  • Trait function

So if the type is Send, then rust chooses inherent function due to the search order. But rust will choose trait function if the type is not Send due to the T: Send bound.

See https://doc.rust-lang.org/reference/expressions/method-call-expr.html (Document describes about methods, but I believe the same rule is applied to associated functions as well)

Here, more specific rules are written.

  1. https://rust-lang.github.io/rfcs/0195-associated-items.html#via-an-id_segment-prefix
  2. https://rust-lang.github.io/rfcs/0195-associated-items.html#via-a-type_segment-prefix
  • 1 tells starting with ID_SEGMENT is equivalent to starting with TYPE_SEGMENT. ‘A::b’ is equivalent to ‘<A>::b’
  • 2 tells inherent members are prioritized over in-scope traits.

Implementations§

Source§

impl<T: ?Sized + Send> TypeHelper<T>

Source

pub const IS_SEND: bool = true

Source§

impl<T: ?Sized + Sync> TypeHelper<T>

Source

pub const IS_SYNC: bool = true

Source§

impl<T: ?Sized + UnwindSafe> TypeHelper<T>

Source

pub const IS_UNWIND_SAFE: bool = true

Source§

impl<T: Debug> TypeHelper<T>

Source

pub const IS_DEBUG: bool = true

Source

pub const FN_FMT: FnFmtRaw

Source

pub const fn fn_fmt() -> FnFmtRaw

Returns a raw function pointer of Debug::fmt for the given type.

But the given type is not Debug, then calling the returned function will cause panic.

Source§

impl<T: Default> TypeHelper<T>

Source

pub const IS_DEFAULT: bool = true

Source

pub const FN_DEFAULT: FnDefaultRaw

Source

pub const fn fn_default() -> FnDefaultRaw

Returns raw function pointer of Default::default for the given type.

But the given type is not Default, then calling the returned function will cause panic.

Source§

impl<T: Clone> TypeHelper<T>

Source

pub const IS_CLONE: bool = true

Source

pub const FN_CLONE: FnCloneRaw

Source

pub const fn fn_clone() -> FnCloneRaw

Returns raw function pointer of Clone::clone for the given type.

But the given type is not Clone, then calling the returned function will cause panic.

Source§

impl<T> TypeHelper<(T, T)>

Source

pub const IS_EQUAL_TYPE: bool = true

Trait Implementations§

Source§

impl<T: ?Sized> NotClone for TypeHelper<T>

Source§

const IS_CLONE: bool = false

Source§

const FN_CLONE: FnCloneRaw = {ds::types::unimpl_clone as unsafe fn(*const u8, *mut u8)}

Source§

impl<T: ?Sized> NotDebug for TypeHelper<T>

Source§

const IS_DEBUG: bool = false

Source§

const FN_FMT: FnFmtRaw = {ds::types::unimpl_fmt as for<'a, 'b> unsafe fn(*const u8, &'a mut std::fmt::Formatter<'b>) -> std::result::Result<(), std::fmt::Error>}

Source§

impl<T: ?Sized> NotDefault for TypeHelper<T>

Source§

const IS_DEFAULT: bool = false

Source§

const FN_DEFAULT: FnDefaultRaw = {ds::types::unimpl_default as unsafe fn(*mut u8)}

Source§

impl<T> NotEqualType for TypeHelper<T>

Source§

impl<T: ?Sized> NotSend for TypeHelper<T>

Source§

const IS_SEND: bool = false

Source§

impl<T: ?Sized> NotSync for TypeHelper<T>

Source§

const IS_SYNC: bool = false

Source§

impl<T: ?Sized> NotUnwindSafe for TypeHelper<T>

Auto Trait Implementations§

§

impl<T> Freeze for TypeHelper<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for TypeHelper<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for TypeHelper<T>
where T: Send + ?Sized,

§

impl<T> Sync for TypeHelper<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for TypeHelper<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for TypeHelper<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.