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
| Property | Type |
|---|---|
reason | string |
Constructors
static new(reason: string) -> ConversionError;
Methods
describe(&this) -> Str;
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
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const wide: i64match (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, and destructuring patterns, and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
static i32::try_from(source: Str) -> Result<i32, ConversionError>via trait TryFrom
std::collections::strconst wide: i64Result::Ok(T)v: i32pattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)e: ConversionErrore: ConversionErrortype struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait From<T> {
static from(value: T) -> This;
}type parameter Tstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static From::from(value: T) -> This(parameter) value: Ttype parameter TThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
type trait Into<T> {
into(&this) -> T;
}Into::into(&this) -> T&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type parameter Ttype trait TryFrom<T> {
static try_from(value: T) -> Result<This, ConversionError>;
}static TryFrom::try_from(value: T) -> Result<This, ConversionError>type parameter Ttype struct ConversionError {
reason: string;
}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
type trait TryInto<T> {
try_into(&this) -> Result<T, ConversionError>;
}TryInto::try_into(&this) -> Result<T, ConversionError>type parameter Ttype struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct ConversionError {
reason: string;
}What went wrong in a TryFrom / TryInto. Carries a short static message describing the failure; richer diagnostics are a caller concern.
ConversionError.reason: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
static ConversionError::new(reason: string) -> ConversionError(parameter) reason: stringConversionError::describe(&this) -> StrA 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.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strvalue as TypeExplicit conversion: between numeric types, or reinterpreting one pointer type as another. Cryo never converts implicitly, and as inserts no range checks - a narrowing cast is the programmer's responsibility.
type trait From<T> {
static from(value: T) -> This;
}type trait TryFrom<T> {
static try_from(value: T) -> Result<This, ConversionError>;
}static From::from(value: T) -> Thisimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype struct IoError {
kind: IoErrorKind;
os_code: i32;
}type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum FmtError {
Io(IoError);
Alloc(AllocError);
}type struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}Into::into(&this) -> Ttype enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
static TryFrom::try_from(value: T) -> Result<This, ConversionError>type trait Into<T> {
into(&this) -> T;
}TryInto::try_into(&this) -> Result<T, ConversionError>ConversionError::describe(&this) -> StrA 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.
ConversionError.reason: stringstatic ConversionError::new(reason: string) -> ConversionErrortype trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}