From 0e3bdb8a2aa262175298909db38d2121fe2235c9 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 4 Sep 2020 14:43:19 -0500 Subject: [PATCH] chore: convert TryFrom error types to &'static str Prevents an unnecessary heap allocation when a RegisterPair::try_from() or Register::try_from() fails. --- src/instruction.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/instruction.rs b/src/instruction.rs index 0214fa5..076e042 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -1165,7 +1165,7 @@ impl From for InstrRegisterPair { } impl TryFrom for RegisterPair { - type Error = String; // FIXME: Proper error type goes here. + type Error = &'static str; // FIXME: Proper error type goes here. fn try_from(pair: InstrRegisterPair) -> Result { match pair { @@ -1176,10 +1176,10 @@ impl TryFrom for RegisterPair { InstrRegisterPair::SP => Ok(Self::SP), InstrRegisterPair::PC => Ok(Self::PC), InstrRegisterPair::IncrementHL => { - Err("Can not convert InstrRegisterPair::IncrementHL to RegisterPair".to_string()) + Err("Can not convert InstrRegisterPair::IncrementHL to RegisterPair") } InstrRegisterPair::DecrementHL => { - Err("Can not convert InstrRegisterPair::DecrementHL to RegisterPair".to_string()) + Err("Can not convert InstrRegisterPair::DecrementHL to RegisterPair") } } } @@ -1199,7 +1199,7 @@ enum InstrRegister { } impl TryFrom for InstrRegister { - type Error = String; // FIXME: Proper error type goes here + type Error = &'static str; // FIXME: Proper error type goes here fn try_from(register: Register) -> Result { match register { @@ -1210,7 +1210,7 @@ impl TryFrom for InstrRegister { Register::E => Ok(Self::E), Register::H => Ok(Self::H), Register::L => Ok(Self::L), - Register::Flag => Err("Can not convert Register::Flag to InstrRegister".to_string()), + Register::Flag => Err("Can not convert Register::Flag to InstrRegister"), } } }