pkstrings: Implement Clone

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2020-06-30 17:18:56 +02:00
parent 8700e56673
commit d3483cc373
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
3 changed files with 11 additions and 8 deletions

View file

@ -101,6 +101,12 @@ impl Deref for PKString {
}
}
impl Clone for PKString {
fn clone(&self) -> PKString {
PKString(self.0.clone())
}
}
impl fmt::Display for PKString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{}", &self.0))

View file

@ -86,9 +86,8 @@ impl TryFrom<&[u8]> for PokemonList {
for i in 1..=count {
let start = PARTY_FIRST_ENTRY_OFFSET + ((i - 1) * PK_FULL_ENTRY_SIZE) - 1;
let end = PARTY_FIRST_ENTRY_OFFSET - 1 + (i * PK_FULL_ENTRY_SIZE) - 1;
// TODO: Reimplement Clone on PKString?
let ot_name: PKString = PKString::try_from(ot_names[i - 1].clone())?;
let pk_nick: PKString = PKString::try_from(pk_nicks[i - 1].clone())?;
let ot_name = ot_names[i - 1].clone();
let pk_nick = pk_nicks[i - 1].clone();
entries.push(IndividualPk::try_from((&data[start..=end], ot_name, pk_nick))?);
}

View file

@ -654,13 +654,11 @@ mod tests {
#[test]
fn test_individual_simple() {
// TODO: Reimplement Clone on PKString?
let ot_name1: PKString = PKString::try_from("FOOBAR").unwrap();
let ot_name2: PKString = PKString::try_from("FOOBAR").unwrap();
let ot_name: PKString = PKString::try_from("FOOBAR").unwrap();
let kadabra_nick: PKString = PKString::try_from("KADABRA").unwrap();
let charizard_nick: PKString = PKString::try_from("CHARIZARD").unwrap();
let pk = IndividualPk::try_from((&KADABRA, ot_name1, kadabra_nick)).unwrap();
let pk = IndividualPk::try_from((&KADABRA, ot_name.clone(), kadabra_nick)).unwrap();
assert_eq!(pk.species, Species::Kadabra);
assert_eq!(pk.current_hp, 44);
assert_eq!(pk.level, 16);
@ -677,7 +675,7 @@ mod tests {
None, None,
].into());
let pk = IndividualPk::try_from((&CHARIZARD, ot_name2, charizard_nick)).unwrap();
let pk = IndividualPk::try_from((&CHARIZARD, ot_name, charizard_nick)).unwrap();
assert_eq!(pk.species, Species::Charizard);
assert_eq!(pk.current_hp, 139);
assert_eq!(pk.level, 42);