From 3d965ae6a479636a13b2e2f2344e8d97bedece1f Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 17 Feb 2022 14:31:02 +0100 Subject: [PATCH] Change default parameter of `Cast` --- src/eval/func.rs | 35 +++++++++++++++++++++++++---------- src/eval/value.rs | 31 ++++++++----------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/eval/func.rs b/src/eval/func.rs index b7b9c9cd3..a4a733b30 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -171,7 +171,10 @@ impl Args { /// /// Returns a `missing argument: {what}` error if no positional argument is /// left. - pub fn expect(&mut self, what: &str) -> TypResult { + pub fn expect(&mut self, what: &str) -> TypResult + where + T: Cast>, + { match self.eat()? { Some(v) => Ok(v), None => bail!(self.span, "missing argument: {}", what), @@ -179,7 +182,10 @@ impl Args { } /// Consume and cast the first positional argument if there is one. - pub fn eat(&mut self) -> TypResult> { + pub fn eat(&mut self) -> TypResult> + where + T: Cast>, + { for (i, slot) in self.items.iter().enumerate() { if slot.name.is_none() { let value = self.items.remove(i).value; @@ -191,7 +197,10 @@ impl Args { } /// Find and consume the first castable positional argument. - pub fn find(&mut self) -> TypResult> { + pub fn find(&mut self) -> TypResult> + where + T: Cast>, + { for (i, slot) in self.items.iter().enumerate() { if slot.name.is_none() && T::is(&slot.value) { let value = self.items.remove(i).value; @@ -203,7 +212,10 @@ impl Args { } /// Find and consume all castable positional arguments. - pub fn all(&mut self) -> TypResult> { + pub fn all(&mut self) -> TypResult> + where + T: Cast>, + { let mut list = vec![]; while let Some(value) = self.find()? { list.push(value); @@ -213,7 +225,10 @@ impl Args { /// Cast and remove the value for the given named argument, returning an /// error if the conversion fails. - pub fn named(&mut self, name: &str) -> TypResult> { + pub fn named(&mut self, name: &str) -> TypResult> + where + T: Cast>, + { // We don't quit once we have a match because when multiple matches // exist, we want to remove all of them and use the last one. let mut i = 0; @@ -231,7 +246,10 @@ impl Args { } /// Same as named, but with fallback to find. - pub fn named_or_find(&mut self, name: &str) -> TypResult> { + pub fn named_or_find(&mut self, name: &str) -> TypResult> + where + T: Cast>, + { match self.named(name)? { Some(value) => Ok(Some(value)), None => self.find(), @@ -266,10 +284,7 @@ impl Args { } /// Reinterpret these arguments as actually being a single castable thing. - fn into_castable(self, what: &str) -> TypResult - where - T: Cast, - { + fn into_castable(self, what: &str) -> TypResult { let mut iter = self.items.into_iter(); let value = match iter.next() { Some(Arg { name: None, value, .. }) => value.v.cast().at(value.span)?, diff --git a/src/eval/value.rs b/src/eval/value.rs index 02ed65580..fffa729a9 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -97,10 +97,7 @@ impl Value { } /// Try to cast the value into a specific type. - pub fn cast(self) -> StrResult - where - T: Cast, - { + pub fn cast(self) -> StrResult { T::cast(self) } @@ -327,7 +324,7 @@ pub trait Type { } /// Cast from a value to a specific type. -pub trait Cast>: Sized { +pub trait Cast: Sized { /// Check whether the value is castable to `Self`. fn is(value: &V) -> bool; @@ -345,7 +342,7 @@ macro_rules! primitive { const TYPE_NAME: &'static str = $name; } - impl Cast for $type { + impl Cast for $type { fn is(value: &Value) -> bool { matches!(value, Value::$variant(_) $(| Value::$other(_))*) } @@ -448,7 +445,7 @@ primitive! { Func: "function", Func, Class(v) => v.constructor() } primitive! { Args: "arguments", Args } primitive! { Class: "class", Class } -impl Cast for Value { +impl Cast for Value { fn is(_: &Value) -> bool { true } @@ -458,10 +455,7 @@ impl Cast for Value { } } -impl Cast for T -where - T: Cast, -{ +impl Cast> for T { fn is(value: &Spanned) -> bool { T::is(&value.v) } @@ -471,10 +465,7 @@ where } } -impl Cast for Spanned -where - T: Cast, -{ +impl Cast> for Spanned { fn is(value: &Spanned) -> bool { T::is(&value.v) } @@ -511,10 +502,7 @@ impl Default for Smart { } } -impl Cast for Option -where - T: Cast, -{ +impl Cast for Option { fn is(value: &Value) -> bool { matches!(value, Value::None) || T::is(value) } @@ -527,10 +515,7 @@ where } } -impl Cast for Smart -where - T: Cast, -{ +impl Cast for Smart { fn is(value: &Value) -> bool { matches!(value, Value::Auto) || T::is(value) }