Skip to content

Commit

Permalink
Replaced TryInto with TryFrom in slint_interpreter::Value
Browse files Browse the repository at this point in the history
Fixes #1258
  • Loading branch information
akiross committed May 13, 2022
1 parent e35bbd3 commit bdceb64
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions internal/interpreter/api.rs
@@ -1,7 +1,7 @@
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial

use core::convert::TryInto;
use core::convert::TryFrom;
use i_slint_compiler::langtype::Type as LangType;
use i_slint_core::graphics::Image;
use i_slint_core::model::{Model, ModelRc};
Expand Down Expand Up @@ -71,7 +71,7 @@ impl From<LangType> for ValueType {

/// This is a dynamically typed value used in the Slint interpreter.
/// It can hold a value of different types, and you should use the
/// [`From`] or [`TryInto`] traits to access the value.
/// [`From`] or [`TryFrom`] traits to access the value.
///
/// ```
/// # use slint_interpreter::*;
Expand Down Expand Up @@ -188,7 +188,7 @@ impl std::fmt::Debug for Value {
}
}

/// Helper macro to implement the From / TryInto for Value
/// Helper macro to implement the From / TryFrom for Value
///
/// For example
/// `declare_value_conversion!(Number => [u32, u64, i32, i64, f32, f64] );`
Expand All @@ -204,13 +204,12 @@ macro_rules! declare_value_conversion {
Value::$value(v as _)
}
}
impl TryInto<$ty> for Value {
impl TryFrom<Value> for $ty {
type Error = Value;
fn try_into(self) -> Result<$ty, Value> {
match self {
//Self::$value(x) => x.try_into().map_err(|_|()),
Self::$value(x) => Ok(x as _),
_ => Err(self)
fn try_from(v: Value) -> Result<$ty, Self::Error> {
match v {
Value::$value(x) => Ok(x as _),
_ => Err(v)
}
}
}
Expand All @@ -227,7 +226,7 @@ declare_value_conversion!(PathData => [PathData]);
declare_value_conversion!(EasingCurve => [i_slint_core::animations::EasingCurve]);
declare_value_conversion!(LayoutCache => [SharedVector<f32>] );

/// Implement From / TryInto for Value that convert a `struct` to/from `Value::Object`
/// Implement From / TryFrom for Value that convert a `struct` to/from `Value::Object`
macro_rules! declare_value_struct_conversion {
(struct $name:path { $($field:ident),* $(, ..$extra:expr)? }) => {
impl From<$name> for Value {
Expand All @@ -237,11 +236,11 @@ macro_rules! declare_value_struct_conversion {
Value::Struct(struct_)
}
}
impl TryInto<$name> for Value {
impl TryFrom<Value> for $name {
type Error = ();
fn try_into(self) -> Result<$name, ()> {
match self {
Self::Struct(x) => {
fn try_from(v: Value) -> Result<$name, Self::Error> {
match v {
Value::Struct(x) => {
type Ty = $name;
Ok(Ty {
$($field: x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?),*
Expand All @@ -263,7 +262,7 @@ declare_value_struct_conversion!(struct i_slint_core::layout::LayoutInfo { min,
declare_value_struct_conversion!(struct i_slint_core::graphics::Point { x, y, ..Default::default()});
declare_value_struct_conversion!(struct i_slint_core::items::PointerEvent { kind, button });

/// Implement From / TryInto for Value that convert an `enum` to/from `Value::EnumerationValue`
/// Implement From / TryFrom for Value that convert an `enum` to/from `Value::EnumerationValue`
///
/// The `enum` must derive `Display` and `FromStr`
/// (can be done with `strum_macros::EnumString`, `strum_macros::Display` derive macro)
Expand All @@ -277,12 +276,12 @@ macro_rules! declare_value_enum_conversion {
)
}
}
impl TryInto<i_slint_core::items::$Name> for Value {
impl TryFrom<Value> for i_slint_core::items::$Name {
type Error = ();
fn try_into(self) -> Result<i_slint_core::items::$Name, ()> {
fn try_from(v: Value) -> Result<i_slint_core::items::$Name, ()> {
use std::str::FromStr;
match self {
Self::EnumerationValue(enumeration, value) => {
match v {
Value::EnumerationValue(enumeration, value) => {
if enumeration != stringify!($Name) {
return Err(());
}
Expand All @@ -309,10 +308,10 @@ impl From<i_slint_core::animations::Instant> for Value {
Value::Number(value.0 as _)
}
}
impl TryInto<i_slint_core::animations::Instant> for Value {
impl TryFrom<Value> for i_slint_core::animations::Instant {
type Error = ();
fn try_into(self) -> Result<i_slint_core::animations::Instant, ()> {
match self {
fn try_from(v: Value) -> Result<i_slint_core::animations::Instant, Self::Error> {
match v {
Value::Number(x) => Ok(i_slint_core::animations::Instant(x as _)),
_ => Err(()),
}
Expand All @@ -325,10 +324,10 @@ impl From<()> for Value {
Value::Void
}
}
impl TryInto<()> for Value {
impl TryFrom<Value> for () {
type Error = ();
#[inline]
fn try_into(self) -> Result<(), ()> {
fn try_from(_: Value) -> Result<(), Self::Error> {
Ok(())
}
}
Expand All @@ -339,13 +338,13 @@ impl From<i_slint_core::Color> for Value {
Value::Brush(Brush::SolidColor(c))
}
}
impl TryInto<i_slint_core::Color> for Value {
impl TryFrom<Value> for i_slint_core::Color {
type Error = Value;
#[inline]
fn try_into(self) -> Result<i_slint_core::Color, Value> {
match self {
fn try_from(v: Value) -> Result<i_slint_core::Color, Self::Error> {
match v {
Value::Brush(Brush::SolidColor(c)) => Ok(c),
_ => Err(self),
_ => Err(v),
}
}
}
Expand All @@ -366,7 +365,7 @@ pub(crate) fn normalize_identifier(ident: &str) -> Cow<'_, str> {
/// written with the `{ key: value, }` notation.
///
/// It can be constructed with the [`FromIterator`] trait, and converted
/// into or from a [`Value`] with the [`From`] and [`TryInto`] trait
/// into or from a [`Value`] with the [`From`], [`TryFrom`] trait
///
///
/// ```
Expand Down

0 comments on commit bdceb64

Please sign in to comment.