From bdceb641204119d8831451bc7cb457a340882a67 Mon Sep 17 00:00:00 2001 From: Alessandro Re Date: Fri, 13 May 2022 08:41:19 +0200 Subject: [PATCH] Replaced TryInto with TryFrom in slint_interpreter::Value Fixes #1258 --- internal/interpreter/api.rs | 57 ++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/internal/interpreter/api.rs b/internal/interpreter/api.rs index f8d6858aa88..f93554c0def 100644 --- a/internal/interpreter/api.rs +++ b/internal/interpreter/api.rs @@ -1,7 +1,7 @@ // Copyright © SixtyFPS GmbH // 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}; @@ -71,7 +71,7 @@ impl From 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::*; @@ -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] );` @@ -204,13 +204,12 @@ macro_rules! declare_value_conversion { Value::$value(v as _) } } - impl TryInto<$ty> for Value { + impl TryFrom 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) } } } @@ -227,7 +226,7 @@ declare_value_conversion!(PathData => [PathData]); declare_value_conversion!(EasingCurve => [i_slint_core::animations::EasingCurve]); declare_value_conversion!(LayoutCache => [SharedVector] ); -/// 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 { @@ -237,11 +236,11 @@ macro_rules! declare_value_struct_conversion { Value::Struct(struct_) } } - impl TryInto<$name> for Value { + impl TryFrom 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(|_|())?),* @@ -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) @@ -277,12 +276,12 @@ macro_rules! declare_value_enum_conversion { ) } } - impl TryInto for Value { + impl TryFrom for i_slint_core::items::$Name { type Error = (); - fn try_into(self) -> Result { + fn try_from(v: Value) -> Result { use std::str::FromStr; - match self { - Self::EnumerationValue(enumeration, value) => { + match v { + Value::EnumerationValue(enumeration, value) => { if enumeration != stringify!($Name) { return Err(()); } @@ -309,10 +308,10 @@ impl From for Value { Value::Number(value.0 as _) } } -impl TryInto for Value { +impl TryFrom for i_slint_core::animations::Instant { type Error = (); - fn try_into(self) -> Result { - match self { + fn try_from(v: Value) -> Result { + match v { Value::Number(x) => Ok(i_slint_core::animations::Instant(x as _)), _ => Err(()), } @@ -325,10 +324,10 @@ impl From<()> for Value { Value::Void } } -impl TryInto<()> for Value { +impl TryFrom for () { type Error = (); #[inline] - fn try_into(self) -> Result<(), ()> { + fn try_from(_: Value) -> Result<(), Self::Error> { Ok(()) } } @@ -339,13 +338,13 @@ impl From for Value { Value::Brush(Brush::SolidColor(c)) } } -impl TryInto for Value { +impl TryFrom for i_slint_core::Color { type Error = Value; #[inline] - fn try_into(self) -> Result { - match self { + fn try_from(v: Value) -> Result { + match v { Value::Brush(Brush::SolidColor(c)) => Ok(c), - _ => Err(self), + _ => Err(v), } } } @@ -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 /// /// /// ```