pub fn default<T: Default>() -> T {
T::default()
}
pub trait OptionWriteTo<T> {
fn write_to(&self, target: &mut T);
}
impl<T> OptionWriteTo<T> for Option<T>
where
T: Clone,
{
fn write_to(&self, target: &mut T) {
if let Some(value) = self {
*target = value.clone();
}
}
}
pub trait OptionMoveTo<T> {
fn write_to(self, target: &mut T);
}
impl<T> OptionMoveTo<T> for Option<T> {
fn write_to(self, target: &mut T) {
if let Some(value) = self {
*target = value;
}
}
}
#[test]
fn option() {
let a: Option<i32> = Some(10);
let mut b = 5;
a.write_to(&mut b);
assert_eq!(b, 10);
let c: Option<String> = Some("hello".to_string());
let mut d = "world".to_string();
OptionWriteTo::write_to(&c, &mut d);
assert_eq!(d, "hello".to_string());
drop(c);
}
#[macro_export]
macro_rules! placeholder_type {
($name:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name;
impl std::default::Default for $name {
fn default() -> Self {
::core::unimplemented!(concat!(
stringify!($name),
" is a placeholder type and cannot be instantiated"
))
}
}
impl $name {
#[allow(unused)]
pub fn new() -> Self {
::core::unimplemented!(concat!(
stringify!($name),
" is a placeholder type and cannot be instantiated"
))
}
}
};
(
$doc:literal,
$name:ident,
$( (fn $mname:ident$($t:tt)*) ),*
) => {
#[doc = $doc]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name;
impl std::default::Default for $name {
fn default() -> Self {
::core::unimplemented!(concat!(
stringify!($name),
" is a placeholder type and cannot be instantiated"
))
}
}
impl $name {
$(
#[allow(unused)]
pub fn $mname $($t)* {
::core::unimplemented!(concat!(
stringify!($name),
" is a placeholder type and cannot be used"
))
}
)*
}
};
(
$doc:literal,
$name:ident
)=>{
placeholder_type!($doc, $name, (fn new()->Self));
}
}
#[test]
fn aa() {
placeholder_type!("随便的测试类型", _T11, (fn foo() -> i32), (fn bar(&self,a: i32)));
#[allow(unreachable_code, unused)]
fn _aa() {
let t: _T11 = todo!();
let _ = _T11::foo();
t.bar(10);
}
}
pub fn write_bmp_rgba(width: u32, height: u32, rgba: &[u8]) -> Vec<u8> {
let w = width as usize;
let h = height as usize;
assert_eq!(rgba.len(), w * h * 4, "rgba 长度必须是 width*height*4");
let row_size = w * 4; let pixel_size = row_size * h;
let file_size = 14 + 40 + pixel_size;
let mut buf = Vec::with_capacity(file_size);
buf.extend_from_slice(b"BM");
buf.extend_from_slice(&(file_size as u32).to_le_bytes());
buf.extend_from_slice(&0u16.to_le_bytes());
buf.extend_from_slice(&0u16.to_le_bytes());
buf.extend_from_slice(&(14u32 + 40u32).to_le_bytes());
buf.extend_from_slice(&40u32.to_le_bytes());
buf.extend_from_slice(&width.to_le_bytes());
buf.extend_from_slice(&(height as i32).to_le_bytes()); buf.extend_from_slice(&1u16.to_le_bytes()); buf.extend_from_slice(&32u16.to_le_bytes()); buf.extend_from_slice(&0u32.to_le_bytes()); buf.extend_from_slice(&(pixel_size as u32).to_le_bytes());
buf.extend_from_slice(&2835u32.to_le_bytes()); buf.extend_from_slice(&2835u32.to_le_bytes()); buf.extend_from_slice(&0u32.to_le_bytes());
buf.extend_from_slice(&0u32.to_le_bytes());
for row in (0..h).rev() {
for col in 0..w {
let i = (row * w + col) * 4;
buf.push(rgba[i + 2]); buf.push(rgba[i + 1]); buf.push(rgba[i]); buf.push(rgba[i + 3]); }
}
buf
}
#[cfg(test)]
mod bmp_tests {
use super::*;
#[test]
fn bmp_header_and_pixel_order() {
let (w, h) = (4u32, 2u32);
let mut rgba = vec![0u8; (w * h * 4) as usize];
for i in 0..4 {
rgba[i * 4..i * 4 + 4].copy_from_slice(&[255, 0, 0, 255]); }
for i in 4..8 {
rgba[i * 4..i * 4 + 4].copy_from_slice(&[0, 0, 255, 128]); }
let bmp = write_bmp_rgba(w, h, &rgba);
assert_eq!(bmp.len(), 14 + 40 + 4 * 2 * 4);
assert_eq!(&bmp[0..2], b"BM");
assert_eq!(&bmp[10..14], &54u32.to_le_bytes());
assert_eq!(&bmp[54..58], &[255, 0, 0, 128]);
assert_eq!(&bmp[70..74], &[0, 0, 255, 255]);
}
}