Function constmuck::try_cast[][src]

pub const fn try_cast<T, U>(
    from: T,
    _bounds: (IsPod<T>, IsPod<U>)
) -> Result<U, PodCastError>
Expand description

Tries to cast T into U

Requires both T and U to implement Pod.

Errors

This returns an Err(PodCastError::SizeMismatch) when T isn’t the same size as U.

Example

use constmuck::PodCastError;
use constmuck::{try_cast, infer};

const OK: Result<i32, PodCastError> = try_cast(u32::MAX, infer!());
const ERR: Result<[u8; 4], PodCastError> = try_cast(100_u16, infer!());

assert_eq!(OK, Ok(-1));
assert_eq!(ERR, Err(PodCastError::SizeMismatch));