Module slint::docs::type_mappings

source ·
Expand description

§Type Mappings

The types used for properties in .slint design markup each translate to specific types in Rust. The follow table summarizes the entire mapping:

.slint TypeRust TypeNote
anglef32The angle in degrees
arrayModelRcArrays are represented as models, so that their contents can change dynamically.
boolbool
brushBrush
colorColor
durationi64At run-time, durations are always represented as signed 64-bit integers with millisecond precision.
floatf32
imageImage
inti32
lengthf32At run-time, logical lengths are automatically translated to physical pixels using the device pixel ratio.
physical_lengthf32The unit are physical pixels.
PointLogicalPositionA struct with x and y fields, representing logical coordinates.
relative-font-sizef32Relative font size factor that is multiplied with the Window.default-font-size and can be converted to a length.
stringSharedStringA reference-counted string type that can be easily converted to a str reference.
anonymous objectanonymous tupleThe fields are in alphabetical order.
enumerationenum of the same nameThe values are converted to CamelCase
structurestruct of the same name

For user defined structures in the .slint, an extra struct is generated. For example, if the .slint contains

export struct MyStruct {
    foo: int,
    bar: string,
    names: [string],
}

The following struct would be generated:

#[derive(Default, Clone, Debug, PartialEq)]
struct MyStruct {
    foo: i32,
    bar: slint::SharedString,
    names: slint::ModelRc<slint::SharedString>,
}

The .slint file allows you to utilize Rust attributes and features for defining structures using the @rust-attr() directive. This enables you to customize the generated code by applying additional traits, derivations, or annotations. Consider the following structure defined in the .slint file with Rust attributes:

@rust-attr(derive(serde::Serialize, serde::Deserialize))
struct MyStruct {
    foo: int,
}

Based on this structure, the following Rust code would be generated:

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(Default, Clone, Debug, PartialEq)]
struct MyStruct {
    foo: i32,
}