1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//---------------------------------------------------------------------------------------------------- Use
use anyhow::{anyhow,bail};
use std::path::PathBuf;
use crate::common;
use crate::header::*;
use bincode::config::*;
//use log::{info,error,warn,trace,debug};
//use serde::{Serialize,Deserialize};
use std::io::{
	Read,Write,
	BufReader,BufWriter,
};
use once_cell::sync::Lazy;

//---------------------------------------------------------------------------------------------------- Bincode
static ENCODING_OPTIONS: Lazy<WithOtherIntEncoding<DefaultOptions, VarintEncoding>> =
		Lazy::new(|| bincode::DefaultOptions::new().with_varint_encoding());

common::impl_macro_binary!(Bincode, "bin");

/// [`Bincode`](https://docs.rs/bincode) (binary) file format
///
/// ## Encoding
/// The encoding option used is:
/// ```rust
/// # use bincode::Options;
/// bincode::DefaultOptions::new().with_varint_encoding();
/// ```
///
/// File extension is `.bin`.
///
/// ## Safety
/// When manually implementing, you are **promising** that the `PATH`'s manually specified are correct.
pub unsafe trait Bincode: serde::Serialize + serde::de::DeserializeOwned {
	#[doc(hidden)]
	#[inline(always)]
	/// Internal function. Most efficient `from_file()` impl.
	fn __from_file() -> Result <Self, anyhow::Error> {
		let path = Self::absolute_path()?;
		let mut file = std::fs::File::open(path)?;
		Self::from_reader(&mut file)
	}

	#[doc(hidden)]
	#[inline(always)]
	/// Internal function. Most efficient `from_path()` impl.
	fn __from_path(path: &std::path::Path) -> Result <Self, anyhow::Error> {
		let mut file = std::fs::File::open(path)?;
		Self::from_reader(&mut file)
	}

	#[inline(always)]
	/// Create a [`Self`] from bytes.
	fn from_bytes(bytes: &[u8]) -> Result<Self, anyhow::Error> {
		ensure_header!(bytes);
		Ok(ENCODING_OPTIONS.deserialize(&bytes[25..])?)
	}

	#[inline(always)]
	/// Convert [`Self`] to bytes.
	fn to_bytes(&self) -> Result<Vec<u8>, anyhow::Error> {
		let mut vec = ENCODING_OPTIONS.serialize(self)?;
		header_return!(vec)
	}

	#[inline(always)]
	/// Create [`Self`] directly from reader `R`.
	fn from_reader<R>(reader: &mut R) -> Result<Self, anyhow::Error>
		where
			R: Read,
	{
		let mut bytes = [0_u8; 25];
		let mut reader = BufReader::new(reader);
		reader.read_exact(&mut bytes)?;
		ensure_header!(bytes);
		Ok(ENCODING_OPTIONS.deserialize_from(&mut reader)?)
	}

	#[inline(always)]
	/// Convert [`Self`] to directly to the writer `W` without intermediate bytes.
	fn to_writer<W>(&self, writer: &mut W) -> Result<(), anyhow::Error>
		where
			W: Write,
	{
		let mut writer = BufWriter::new(writer);
		writer.write_all(&Self::full_header())?;
		Ok(ENCODING_OPTIONS.serialize_into(&mut writer, self)?)
	}

	impl_header!();
	common::impl_binary!("bincode");
}


//---------------------------------------------------------------------------------------------------- TESTS
//#[cfg(test)]
//mod tests {
//}