implement correct Hash for FString so indexing and hash maps would be working as intended

This commit is contained in:
mykola2312 2023-08-31 13:51:13 +03:00
parent 4df2803fdb
commit 4d78a5ca8b
2 changed files with 9 additions and 4 deletions

View file

@ -5,6 +5,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use encoding_rs::WINDOWS_1251; use encoding_rs::WINDOWS_1251;
use std::io::Cursor; use std::io::Cursor;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::fmt; use std::fmt;
// FString - Fallout // FString - Fallout
@ -15,7 +16,7 @@ pub enum FStringEncoding {
WCS2, WCS2,
} }
#[derive(Debug, Hash, Eq, Clone)] #[derive(Debug, Eq, Clone)]
pub struct FString { pub struct FString {
pub encoding: FStringEncoding, pub encoding: FStringEncoding,
pub enc_len: usize, pub enc_len: usize,
@ -88,6 +89,12 @@ impl PartialEq for FString {
} }
} }
impl Hash for FString {
fn hash<H: Hasher>(&self, state: &mut H) {
self.str.hash(state);
}
}
impl Borrow<str> for FString { impl Borrow<str> for FString {
fn borrow(&self) -> &str { fn borrow(&self) -> &str {
self.str.as_str() self.str.as_str()
@ -96,6 +103,6 @@ impl Borrow<str> for FString {
impl fmt::Display for FString { impl fmt::Display for FString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\"{}\"", self.str) write!(f, "{}", self.str)
} }
} }

View file

@ -1,7 +1,6 @@
use super::decoder::Decoder; use super::decoder::Decoder;
use super::esh::ESH; use super::esh::ESH;
use super::fstring::FString; use super::fstring::FString;
use super::fstring::FStringEncoding;
use super::raw::Raw; use super::raw::Raw;
use super::sgd::SGD; use super::sgd::SGD;
use super::ssg::SSG; use super::ssg::SSG;
@ -13,7 +12,6 @@ use byteorder::{LittleEndian, WriteBytesExt};
use deflate::deflate_bytes_zlib; use deflate::deflate_bytes_zlib;
use inflate::inflate_bytes_zlib; use inflate::inflate_bytes_zlib;
use std::io::Cursor; use std::io::Cursor;
use std::path::Path;
#[derive(Debug)] #[derive(Debug)]
pub struct World { pub struct World {