Skip to main content

timecat/nnue/
nnue_utils.rs

1use super::*;
2
3#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
5pub struct BinaryMagic<T> {
6    architecture: T,
7}
8
9impl<T> Deref for BinaryMagic<T> {
10    type Target = T;
11
12    fn deref(&self) -> &Self::Target {
13        &self.architecture
14    }
15}
16
17impl<T: Debug> Debug for BinaryMagic<T> {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "{:?}", self.architecture)
20    }
21}
22
23impl<T: BinRead<Args = ()> + Copy + PartialEq + Send + Sync + 'static> BinRead for BinaryMagic<T> {
24    type Args = (T,);
25
26    fn read_options<R: Read + Seek>(
27        reader: &mut R,
28        options: &binread::ReadOptions,
29        (magic,): Self::Args,
30    ) -> BinResult<Self> {
31        let architecture = BinRead::read_options(reader, options, ())?;
32        if architecture == magic {
33            Ok(Self { architecture })
34        } else {
35            Err(binread::Error::BadMagic {
36                pos: reader.stream_position()?,
37                found: Box::new(architecture),
38            })
39        }
40    }
41}