fix support for language switch

update document
This commit is contained in:
2022-05-15 12:26:58 +08:00
parent 50122f6450
commit 76f6691f8a
5 changed files with 127 additions and 17 deletions

View File

@@ -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>;

View File

@@ -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
View 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)));
}
}
}
}
}