minor performance improvements

Changed a couple cases where the inner Rc object was cloned to use the Rc object instead
This commit is contained in:
Kasey White 2023-02-01 14:20:15 -05:00 committed by Lucas
parent 99c1c880b0
commit 456b08a205
2 changed files with 29 additions and 10 deletions

View File

@ -680,6 +680,18 @@ impl TryFrom<Value> for Type {
}
}
impl TryFrom<&Value> for Type {
type Error = Error;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
let constant: Constant = value.try_into()?;
let constant_type = Type::from(&constant);
Ok(constant_type)
}
}
impl TryFrom<Value> for Constant {
type Error = Error;
@ -691,6 +703,17 @@ impl TryFrom<Value> for Constant {
}
}
impl TryFrom<&Value> for Constant {
type Error = Error;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Con(constant) => Ok(constant.as_ref().clone()),
rest => Err(Error::NotAConstant(rest.clone())),
}
}
}
impl From<&Constant> for Type {
fn from(constant: &Constant) -> Self {
match constant {

View File

@ -279,7 +279,7 @@ impl DefaultFunction {
if args.is_empty() {
Ok(())
} else {
let first = args[0].as_ref().clone();
let first = args[0].as_ref();
arg.expect_type(Type::List(Rc::new(first.try_into()?)))
}
@ -675,9 +675,9 @@ impl DefaultFunction {
DefaultFunction::AppendString => match (args[0].as_ref(), args[1].as_ref()) {
(Value::Con(string1), Value::Con(string2)) => {
match (string1.as_ref(), string2.as_ref()) {
(Constant::String(arg1), Constant::String(arg2)) => Ok(Value::Con(
Constant::String(format!("{arg1}{arg2}")).into(),
)),
(Constant::String(arg1), Constant::String(arg2)) => {
Ok(Value::Con(Constant::String(format!("{arg1}{arg2}")).into()))
}
_ => unreachable!(),
}
}
@ -749,18 +749,14 @@ impl DefaultFunction {
},
DefaultFunction::FstPair => match args[0].as_ref() {
Value::Con(pair) => match pair.as_ref() {
Constant::ProtoPair(_, _, first, _) => {
Ok(Value::Con(first.as_ref().clone().into()))
}
Constant::ProtoPair(_, _, first, _) => Ok(Value::Con(first.clone())),
_ => unreachable!(),
},
_ => unreachable!(),
},
DefaultFunction::SndPair => match args[0].as_ref() {
Value::Con(pair) => match pair.as_ref() {
Constant::ProtoPair(_, _, _, second) => {
Ok(Value::Con(second.as_ref().clone().into()))
}
Constant::ProtoPair(_, _, _, second) => Ok(Value::Con(second.clone())),
_ => unreachable!(),
},
_ => unreachable!(),