mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
update
This commit is contained in:
53
src/ext/io.rs
Normal file
53
src/ext/io.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use proc_macros::define_struct_reader_fn;
|
||||
use proc_macros::impl_struct_reader_read;
|
||||
use std::io::Read;
|
||||
|
||||
/// Read number.
|
||||
pub trait StructRead {
|
||||
/// The error type
|
||||
type Error;
|
||||
define_struct_reader_fn!(u8);
|
||||
define_struct_reader_fn!(i8);
|
||||
define_struct_reader_fn!(u16);
|
||||
define_struct_reader_fn!(i16);
|
||||
define_struct_reader_fn!(u32);
|
||||
define_struct_reader_fn!(i32);
|
||||
define_struct_reader_fn!(u64);
|
||||
define_struct_reader_fn!(i64);
|
||||
define_struct_reader_fn!(usize);
|
||||
define_struct_reader_fn!(isize);
|
||||
define_struct_reader_fn!(u128);
|
||||
define_struct_reader_fn!(i128);
|
||||
/// Read exact number of bytes.
|
||||
/// * `size` - The number of bytes
|
||||
///
|
||||
/// Returns io error or the bytes.
|
||||
fn read_bytes(&mut self, size: usize) -> Result<Vec<u8>, Self::Error>;
|
||||
}
|
||||
|
||||
impl<T: Read> StructRead for T {
|
||||
type Error = std::io::Error;
|
||||
impl_struct_reader_read!(u8);
|
||||
impl_struct_reader_read!(i8);
|
||||
impl_struct_reader_read!(u16);
|
||||
impl_struct_reader_read!(i16);
|
||||
impl_struct_reader_read!(u32);
|
||||
impl_struct_reader_read!(i32);
|
||||
impl_struct_reader_read!(u64);
|
||||
impl_struct_reader_read!(i64);
|
||||
impl_struct_reader_read!(usize);
|
||||
impl_struct_reader_read!(isize);
|
||||
impl_struct_reader_read!(u128);
|
||||
impl_struct_reader_read!(i128);
|
||||
|
||||
fn read_bytes(&mut self, size: usize) -> Result<Vec<u8>, Self::Error> {
|
||||
let mut h = self.take(size as u64);
|
||||
let mut r = Vec::new();
|
||||
let s = h.read_to_end(&mut r)?;
|
||||
if s != size {
|
||||
Err(std::io::Error::from(std::io::ErrorKind::UnexpectedEof))
|
||||
} else {
|
||||
Ok(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
pub mod cstr;
|
||||
#[cfg(feature = "flagset")]
|
||||
pub mod flagset;
|
||||
pub mod io;
|
||||
pub mod json;
|
||||
#[cfg(any(feature = "exif", feature = "avdict", feature = "ugoira"))]
|
||||
pub mod rawhandle;
|
||||
pub mod replace;
|
||||
pub mod rw_lock;
|
||||
pub mod try_err;
|
||||
pub mod use_or_not;
|
||||
|
||||
82
src/ext/replace.rs
Normal file
82
src/ext/replace.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use crate::ext::rw_lock::GetRwLock;
|
||||
use std::ops::DerefMut;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::RwLockWriteGuard;
|
||||
|
||||
/// Replace current value with another value
|
||||
pub trait ReplaceWith<T> {
|
||||
/// Replace current value with another value
|
||||
/// * `another` - another value
|
||||
///
|
||||
/// Returns the old value.
|
||||
fn replace_with(&mut self, another: T) -> T;
|
||||
}
|
||||
|
||||
/// Replace current value with another value
|
||||
///
|
||||
/// If you want to mutably borrows, please use [ReplaceWith] instead.
|
||||
pub trait ReplaceWith2<T> {
|
||||
/// Replace current value with another value
|
||||
/// * `another` - another value
|
||||
///
|
||||
/// Returns the old value.
|
||||
fn replace_with2(&self, another: T) -> T;
|
||||
}
|
||||
|
||||
impl<T> ReplaceWith<T> for T {
|
||||
#[inline]
|
||||
fn replace_with(&mut self, another: T) -> T {
|
||||
std::mem::replace(self, another)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> ReplaceWith<T> for RwLockWriteGuard<'a, T> {
|
||||
#[inline]
|
||||
fn replace_with(&mut self, another: T) -> T {
|
||||
self.deref_mut().replace_with(another)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ReplaceWith<T> for RwLock<T> {
|
||||
#[inline]
|
||||
fn replace_with(&mut self, another: T) -> T {
|
||||
self.replace_with2(another)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ReplaceWith2<T> for RwLock<T> {
|
||||
#[inline]
|
||||
fn replace_with2(&self, another: T) -> T {
|
||||
self.get_mut().replace_with(another)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_replace_with_atomic {
|
||||
($type1:ty, $type2:ty) => {
|
||||
impl ReplaceWith<$type2> for $type1 {
|
||||
#[inline]
|
||||
fn replace_with(&mut self, another: $type2) -> $type2 {
|
||||
self.replace_with2(another)
|
||||
}
|
||||
}
|
||||
impl ReplaceWith2<$type2> for $type1 {
|
||||
fn replace_with2(&self, another: $type2) -> $type2 {
|
||||
let ori = self.load(std::sync::atomic::Ordering::Relaxed);
|
||||
self.store(another, std::sync::atomic::Ordering::Relaxed);
|
||||
ori
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicBool, bool);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicI8, i8);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicU8, u8);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicI16, i16);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicU16, u16);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicI32, i32);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicU32, u32);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicI64, i64);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicU64, u64);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicIsize, isize);
|
||||
impl_replace_with_atomic!(std::sync::atomic::AtomicUsize, usize);
|
||||
Reference in New Issue
Block a user