zig-std 0.1.1

Bindings to Zig `std`, with optional Rust `std` features
Documentation
use crate::util::{ ReprCSlice, sys };

sys! {
	pub fn zig_std_rs__crypto__pwhash__argon2__calc_size(params: Params) -> usize;
}

#[repr(u8)]
pub enum Mode {
	Argon2d = 0,
	Argon2i = 1,
	Argon2id = 2
}

#[repr(C)]
pub struct Params {
	t: u32,
	m: u32,
	p: u32,

	secret: ReprCSlice<u8>,
	ad: ReprCSlice<u8>
}

#[inline]
pub fn calc_size(params: Params) -> usize {
	unsafe { sys::zig_std_rs__crypto__pwhash__argon2__calc_size(params) }
}

// size/align assertions
const _: () = {
	assert!(size_of::<Mode>() == 1);
	assert!(align_of::<Mode>() == 1);
	// 4 * size of u32 because of padding for alignment
	// 4 * size of usize because each ReprCSlice is 2 * usize
	assert!(size_of::<Params>() == (4 * size_of::<u32>()) + (4 * size_of::<usize>()));
	assert!(align_of::<Params>() == align_of::<usize>());
};

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_calc_size() {
		// todo we could maybe improve this?

		let params = Params {
			t: 4,
			m: 0x10000,
			p: 2,
			secret: ReprCSlice::null(),
			ad: ReprCSlice::null()
		};
		let expected = 67108864;
		let size = calc_size(params);

		assert_eq!(expected, size);
	}
}