Skip to content
CryoCryo home
Stdlibcore

convert

import std::core::convert; · source

Cryo has no implicit conversions. Every change of type is either an as cast or a conversion method, and the four traits here are the conversion methods.

import std::core::convert;

const wide: i64 = i64::from(narrow);
match (i32::try_from(wide)) {
    Result::Ok(v)  => { /* fits */ }
    Result::Err(e) => { eprintln(f"{e}"); }
}

Implement From when the conversion is always defined (i32 to i64, where every value fits) and TryFrom when it isn't (i64 to i32, where it might not).

Types

From<T>

type trait From<T> {
    static from(value: T) -> This;
}

Build This from a T. Always succeeds. The widening conversions — every pair that cannot lose information — ship as From.

Constructors

static from(value: T) -> This;

Implementors

implement trait From<i8> for i16
implement trait From<i8> for i32
implement trait From<i8> for i64
implement trait From<i16> for i32
implement trait From<i16> for i64
implement trait From<i32> for i64
implement trait From<u8> for u16
implement trait From<u8> for u32
implement trait From<u8> for u64
implement trait From<u16> for u32
implement trait From<u16> for u64
implement trait From<u32> for u64
implement trait From<u8> for i16
implement trait From<u8> for i32
implement trait From<u8> for i64
implement trait From<f32> for f64
implement trait From<IoError> for enum FmtError
std::fmt::error
implement trait From<AllocError> for enum FmtError
std::fmt::error

Into<T>

type trait Into<T> {
    into(&this) -> T;
}

Consume This, produce a T. Always succeeds. The mirror of From, for call sites where the source is in hand and the destination type is inferred.

Methods

into(&this) -> T;

TryFrom<T>

type trait TryFrom<T> {
    static try_from(value: T) -> Result<This, ConversionError>;
}

The fallible counterpart of From, returning Result<This, ConversionError>. The narrowing and sign-crossing numeric conversions ship as TryFrom; Str adds TryFrom<Str> for every integer width — that is how text is parsed — and json adds TryFrom<&JsonValue> for the scalar types.

Constructors

static try_from(value: T) -> Result<This, ConversionError>;

Implementors

implement trait TryFrom<i32> for i16
implement trait TryFrom<i64> for i32
implement trait TryFrom<u64> for u32
implement trait TryFrom<i64> for u64
implement trait TryFrom<u64> for i64
implement trait TryFrom<Str> for u64
std::collections::str
implement trait TryFrom<Str> for u32
std::collections::str
implement trait TryFrom<Str> for u16
std::collections::str
implement trait TryFrom<Str> for u8
std::collections::str
implement trait TryFrom<Str> for i64
std::collections::str
implement trait TryFrom<Str> for i32
std::collections::str
implement trait TryFrom<Str> for i16
std::collections::str
implement trait TryFrom<Str> for i8
std::collections::str
implement trait TryFrom<&JsonValue> for boolean
std::json::value
implement trait TryFrom<&JsonValue> for i64
std::json::value
implement trait TryFrom<&JsonValue> for u64
std::json::value
implement trait TryFrom<&JsonValue> for f64
std::json::value
implement trait TryFrom<&JsonValue> for Str
std::json::value

TryInto<T>

type trait TryInto<T> {
    try_into(&this) -> Result<T, ConversionError>;
}

The fallible counterpart of Into.

Methods

try_into(&this) -> Result<T, ConversionError>;

ConversionError

type struct ConversionError {
    reason: string;

    static new(reason: string) -> ConversionError;
    describe(&this) -> Str;
}

Carries a short static message, reachable through describe(&this) -> Str.

Properties

PropertyType
reasonstring

Constructors

static new(reason: string) -> ConversionError;

Methods

describe(&this) -> Str;

A short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for ConversionError delegate to it. Backed by the static reason literal, so the view outlives any ConversionError.

Trait implementations

implement trait Display for struct ConversionError
std::fmt::display
implement trait Debug for struct ConversionError
std::fmt::display