mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
fix support for language switch
update document
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use json::JsonValue;
|
||||
use std::ops::Deref;
|
||||
use std::sync::RwLockReadGuard;
|
||||
use std::sync::RwLockWriteGuard;
|
||||
|
||||
pub trait ToJson {
|
||||
fn to_json(&self) -> Option<JsonValue>;
|
||||
@@ -28,6 +31,27 @@ impl<T: ToJson> ToJson for &T {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToJson> ToJson for Option<T> {
|
||||
fn to_json(&self) -> Option<JsonValue> {
|
||||
match self {
|
||||
Some(d) => { d.to_json() }
|
||||
None => { None }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToJson> ToJson for RwLockReadGuard<'_, T> {
|
||||
fn to_json(&self) -> Option<JsonValue> {
|
||||
self.deref().to_json()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToJson> ToJson for RwLockWriteGuard<'_, T> {
|
||||
fn to_json(&self) -> Option<JsonValue> {
|
||||
self.deref().to_json()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromJson where Self: Sized {
|
||||
type Err;
|
||||
fn from_json<T: ToJson>(v: T) -> Result<Self, Self::Err>;
|
||||
|
||||
@@ -4,5 +4,6 @@ pub mod flagset;
|
||||
pub mod json;
|
||||
#[cfg(any(feature = "exif", feature = "avdict", feature = "ugoira"))]
|
||||
pub mod rawhandle;
|
||||
pub mod rw_lock;
|
||||
pub mod try_err;
|
||||
pub mod use_or_not;
|
||||
|
||||
35
src/ext/rw_lock.rs
Normal file
35
src/ext/rw_lock.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use spin_on::spin_on;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::RwLockReadGuard;
|
||||
use std::sync::RwLockWriteGuard;
|
||||
use std::time::Duration;
|
||||
|
||||
pub trait GetRwLock {
|
||||
type Target;
|
||||
fn get_ref(&self) -> RwLockReadGuard<Self::Target>;
|
||||
fn get_mut(&self) -> RwLockWriteGuard<Self::Target>;
|
||||
}
|
||||
|
||||
impl<T: Sized> GetRwLock for RwLock<T> {
|
||||
type Target = T;
|
||||
fn get_ref<'a>(&'a self) -> RwLockReadGuard<'a, Self::Target> {
|
||||
loop {
|
||||
match self.try_read() {
|
||||
Ok(f) => { return f; }
|
||||
Err(_) => {
|
||||
spin_on(tokio::time::sleep(Duration::new(0, 1_000_000)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn get_mut<'a>(&'a self) -> RwLockWriteGuard<'a, Self::Target> {
|
||||
loop {
|
||||
match self.try_write() {
|
||||
Ok(f) => { return f; }
|
||||
Err(_) => {
|
||||
spin_on(tokio::time::sleep(Duration::new(0, 1_000_000)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user