HashMapper

Struct HashMapper 

Source
pub struct HashMapper<T, const NBITS: usize>
where T: Hash,
{ /* private fields */ }
Expand description

Store hash-to-string association for a hash value

A hash mapping can be loaded from and written to files. Such files store one line per hash, formatted as <hex-value> <string>.

Implementations§

Source§

impl<T, const N: usize> HashMapper<T, N>
where T: Eq + Hash + Copy,

Source

pub fn new() -> Self

Create a new, empty mapping

Source

pub fn get(&self, hash: T) -> Option<&str>

Get a value from the mapping

Source

pub fn seek(&self, hash: T) -> HashOrStr<T, &str>

Return a matching string (if known) or the hash

Use this method to get a string representation with a fallback for unknown hashes.

let mut mapper = HashMapper::<u16>::new();
mapper.insert(42, "forty-two".to_string());
assert_eq!(format!("{}", mapper.seek(42)), "forty-two");
assert_eq!(format!("{}", mapper.seek(0x1234)), "{1234}");
Source

pub fn is_empty(&self) -> bool

Return true if the mapping is empty

Source

pub fn is_known(&self, hash: T) -> bool

Return true if the given hash is known

Source

pub fn insert(&mut self, hash: T, value: String)

Add a hash to the mapper

Important: the caller must ensure the value matches the hash.

Source§

impl<T, const N: usize> HashMapper<T, N>
where T: Num + Eq + Hash + Copy,

Source

pub fn from_reader<R: BufRead>(reader: R) -> Result<Self, HashError>

Create a new mapping, loaded from a reader

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, HashError>

Create a new mapping, loaded from a file

Source

pub fn load_reader<R: BufRead>(&mut self, reader: R) -> Result<(), HashError>

Load hash mapping from a reader

Source

pub fn load_path<P: AsRef<Path>>(&mut self, path: P) -> Result<(), HashError>

Load hash mapping from a file

Source§

impl<T, const N: usize> HashMapper<T, N>
where T: Eq + Hash + Copy + LowerHex,

Source

pub fn write<W: Write>(&self, writer: &mut W) -> Result<()>

Write hash mapping to a writer

Source

pub fn write_path<P: AsRef<Path>>(&self, path: P) -> Result<()>

Write hash map to a file

File is upadeted atomically.

Trait Implementations§

Source§

impl<T, const N: usize> Debug for HashMapper<T, N>
where T: Hash,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const NBITS: usize> Default for HashMapper<T, NBITS>
where T: Hash + Default,

Source§

fn default() -> HashMapper<T, NBITS>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, const NBITS: usize> Freeze for HashMapper<T, NBITS>

§

impl<T, const NBITS: usize> RefUnwindSafe for HashMapper<T, NBITS>
where T: RefUnwindSafe,

§

impl<T, const NBITS: usize> Send for HashMapper<T, NBITS>
where T: Send,

§

impl<T, const NBITS: usize> Sync for HashMapper<T, NBITS>
where T: Sync,

§

impl<T, const NBITS: usize> Unpin for HashMapper<T, NBITS>
where T: Unpin,

§

impl<T, const NBITS: usize> UnwindSafe for HashMapper<T, NBITS>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V