@@ -248,16 +248,14 @@ pub trait ScalarValue:
248248 ///
249249 /// ```rust
250250 /// # use arcstr::ArcStr;
251- /// # use juniper::{GraphQLScalar, Scalar, ScalarValue, TryScalarValueTo, Value};
251+ /// # use juniper::{FieldResult, GraphQLScalar, Scalar, ScalarValue, TryScalarValueTo, Value};
252252 /// #
253253 /// #[derive(GraphQLScalar)]
254254 /// #[graphql(from_input_with = Self::from_input, transparent)]
255255 /// struct Name(ArcStr);
256256 ///
257257 /// impl Name {
258- /// fn from_input<S: ScalarValue>(
259- /// v: &Scalar<S>,
260- /// ) -> Result<Self, <S as TryScalarValueTo<'_, &str>>::Error> {
258+ /// fn from_input<S: ScalarValue>(v: &Scalar<S>) -> FieldResult<Self, S> {
261259 /// // Check if our `ScalarValue` is represented by an `ArcStr` already, and if so,
262260 /// // do the cheap `Clone` instead of allocating a new `ArcStr` in its `From<&str>`
263261 /// // implementation.
@@ -302,12 +300,11 @@ pub trait ScalarValue:
302300 ///
303301 /// Implementations should not implement this method, but rather implement the
304302 /// [`TryScalarValueTo<T>`] conversion directly.
305- fn try_to < ' a , T > ( & ' a self ) -> Result < T , < Self as TryScalarValueTo < ' a , Scalar < T > > > :: Error >
303+ fn try_to < ' a , T > ( & ' a self ) -> FieldResult < T , Self >
306304 where
307- T : ' a ,
308- Self : TryScalarValueTo < ' a , Scalar < T > , Error : IntoFieldError < Self > > ,
305+ T : FromScalarValue < ' a , Self > + ' a ,
309306 {
310- self . try_scalar_value_to ( ) . map ( | s : Scalar < T > | s . 0 )
307+ T :: from_scalar_value ( self )
311308 }
312309
313310 /// Tries to represent this [`ScalarValue`] as a [`bool`] value.
@@ -324,7 +321,7 @@ pub trait ScalarValue:
324321 /// [`TryScalarValueTo<bool>`] conversions for all the supported boolean types.
325322 #[ must_use]
326323 fn try_to_bool ( & self ) -> Option < bool > {
327- self . try_to ( ) . ok ( )
324+ self . try_scalar_value_to ( ) . ok ( )
328325 }
329326
330327 /// Tries to represent this [`ScalarValue`] as an [`i32`] value.
@@ -342,7 +339,7 @@ pub trait ScalarValue:
342339 /// less to an integer, if requested.
343340 #[ must_use]
344341 fn try_to_int ( & self ) -> Option < i32 > {
345- self . try_to ( ) . ok ( )
342+ self . try_scalar_value_to ( ) . ok ( )
346343 }
347344
348345 /// Tries to represent this [`ScalarValue`] as a [`f64`] value.
@@ -360,7 +357,7 @@ pub trait ScalarValue:
360357 /// all floating point values with 64 bit or less to a float, if requested.
361358 #[ must_use]
362359 fn try_to_float ( & self ) -> Option < f64 > {
363- self . try_to ( ) . ok ( )
360+ self . try_scalar_value_to ( ) . ok ( )
364361 }
365362
366363 /// Tries to represent this [`ScalarValue`] as a [`String`] value.
@@ -380,7 +377,7 @@ pub trait ScalarValue:
380377 /// [`TryScalarValueTo<String>`] conversions for all the supported string types, if requested.
381378 #[ must_use]
382379 fn try_to_string ( & self ) -> Option < String > {
383- self . try_to ( ) . ok ( )
380+ self . try_scalar_value_to ( ) . ok ( )
384381 }
385382
386383 /// Tries to convert this [`ScalarValue`] into a [`String`] value.
@@ -417,7 +414,7 @@ pub trait ScalarValue:
417414 /// requested.
418415 #[ must_use]
419416 fn try_as_str ( & self ) -> Option < & str > {
420- self . try_to ( ) . ok ( )
417+ self . try_scalar_value_to ( ) . ok ( )
421418 }
422419
423420 /// Converts this [`ScalarValue`] into another one via [`i32`], [`f64`], [`bool`] or [`String`]
@@ -496,25 +493,33 @@ impl<'me, S: ScalarValue> TryScalarValueTo<'me, &'me Scalar<S>> for S {
496493 }
497494}
498495
499- impl < ' me , S : ScalarValue > TryScalarValueTo < ' me , Scalar < & ' me str > > for S {
496+ impl < ' me , T , S > TryScalarValueTo < ' me , Scalar < T > > for S
497+ where
498+ T : FromScalarValue < ' me , S > + ' me ,
499+ S : ScalarValue ,
500+ {
500501 type Error = FieldError < Self > ;
501502
502- fn try_scalar_value_to ( & ' me self ) -> FieldResult < Scalar < & ' me str > , Self > {
503- self . try_scalar_value_to ( ) . map ( Scalar )
503+ fn try_scalar_value_to ( & ' me self ) -> FieldResult < Scalar < T > , Self > {
504+ T :: from_scalar_value ( self ) . map ( Scalar )
504505 }
505506}
506507
507- impl < ' me , T : FromScalarValue < S > + ' me , S : ScalarValue > TryScalarValueTo < ' me , Scalar < T > > for S {
508- type Error = FieldError < Self > ;
508+ pub trait FromScalarValue < ' s , S : ' s = DefaultScalarValue > : Sized {
509+ /// Performs the conversion.
510+ fn from_scalar_value ( v : & ' s S ) -> FieldResult < Self , S > ;
511+ }
509512
510- fn try_scalar_value_to ( & ' me self ) -> FieldResult < Scalar < T > , Self > {
511- T :: from_scalar_value ( self ) . map ( Scalar )
513+ impl < ' s , S > FromScalarValue < ' s , S > for & ' s S {
514+ fn from_scalar_value ( v : & ' s S ) -> FieldResult < Self , S > {
515+ Ok ( v)
512516 }
513517}
514518
515- pub trait FromScalarValue < S = DefaultScalarValue > : Sized {
516- /// Performs the conversion.
517- fn from_scalar_value ( v : & S ) -> FieldResult < Self , S > ;
519+ impl < ' s , S > FromScalarValue < ' s , S > for & ' s Scalar < S > {
520+ fn from_scalar_value ( v : & ' s S ) -> FieldResult < Self , S > {
521+ Ok ( Scalar :: ref_cast ( v) )
522+ }
518523}
519524
520525/// Error of a [`ScalarValue`] not matching the expected type.
0 commit comments