Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Sep 17, 2024
1 parent 6f0edf4 commit d9c86ed
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/voicevox_core_java_api/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<T: HasJavaClassIdent + 'static> Closable<T> {
impl<T: HasJavaClassIdent> Drop for Closable<T> {
fn drop(&mut self) {
let content = mem::replace(
&mut *self.0.write().unwrap_or_else(|e| panic!("{e}")),
self.0.get_mut().unwrap_or_else(|e| panic!("{e}")),
MaybeClosed::Closed,
);
if let MaybeClosed::Open(content) = content {
Expand Down
1 change: 0 additions & 1 deletion crates/voicevox_core_python_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish.workspace = true
crate-type = ["cdylib"]

[dependencies]
blocking.workspace = true
camino.workspace = true
easy-ext.workspace = true
futures-lite.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_python_api/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ pub(crate) fn to_rust_word_type(word_type: &PyAny) -> PyResult<UserDictWordType>
/// おおよそ以下のコードにおける`f(x)`のようなものを得る。
///
/// ```py
/// async def f(x):
/// return x
/// async def f(x_):
/// return x_
///
/// return f(x)
/// ```
Expand Down
30 changes: 13 additions & 17 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<T: 'static, C: PyTypeInfo> Closable<T, C, Tokio> {

impl<T: 'static, C: PyTypeInfo, A: Async> Drop for Closable<T, C, A> {
fn drop(&mut self) {
let content = mem::replace(&mut *self.content.blocking_write_(), MaybeClosed::Closed);
let content = mem::replace(self.content.get_mut_(), MaybeClosed::Closed);
if matches!(content, MaybeClosed::Open(_)) {
warn!(
"デストラクタにより`{}`のクローズが行います。通常は、可能な限り`{}`でクローズする\
Expand Down Expand Up @@ -187,34 +187,27 @@ impl Async for Tokio {
type RwLock<T: 'static> = tokio::sync::RwLock<T>;
}

// `'static`制約はouroborosのため
trait RwLock: From<Self::Item> + 'static {
type Item: 'static;
type RwLockReadGuard<'a>: Deref<Target = Self::Item>;
type RwLockWriteGuard<'a>: DerefMut<Target = Self::Item>;
fn try_read_(&self) -> Result<Self::RwLockReadGuard<'_>, ()>;
fn try_read_(&self) -> Result<impl Deref<Target = Self::Item>, ()>;
async fn write_(&self) -> Self::RwLockWriteGuard<'_>;
fn blocking_write_(&self) -> Self::RwLockWriteGuard<'_>;
fn try_write_(&self) -> Result<Self::RwLockWriteGuard<'_>, ()>;
fn get_mut_(&mut self) -> &mut Self::Item;
}

impl<T: 'static> RwLock for std::sync::RwLock<T> {
type Item = T;
type RwLockReadGuard<'a> = std::sync::RwLockReadGuard<'a, Self::Item>;
type RwLockWriteGuard<'a> = std::sync::RwLockWriteGuard<'a, Self::Item>;

fn try_read_(&self) -> Result<Self::RwLockReadGuard<'_>, ()> {
fn try_read_(&self) -> Result<impl Deref<Target = Self::Item>, ()> {
self.try_read().map_err(|e| match e {
std::sync::TryLockError::Poisoned(e) => panic!("{e}"),
std::sync::TryLockError::WouldBlock => (),
})
}

async fn write_(&self) -> Self::RwLockWriteGuard<'_> {
self.blocking_write_()
}

fn blocking_write_(&self) -> Self::RwLockWriteGuard<'_> {
self.write().unwrap_or_else(|e| panic!("{e}"))
}

Expand All @@ -224,28 +217,31 @@ impl<T: 'static> RwLock for std::sync::RwLock<T> {
std::sync::TryLockError::WouldBlock => (),
})
}

fn get_mut_(&mut self) -> &mut Self::Item {
self.get_mut().unwrap_or_else(|e| panic!("{e}"))
}
}

impl<T: 'static> RwLock for tokio::sync::RwLock<T> {
type Item = T;
type RwLockReadGuard<'a> = tokio::sync::RwLockReadGuard<'a, Self::Item>;
type RwLockWriteGuard<'a> = tokio::sync::RwLockWriteGuard<'a, Self::Item>;

fn try_read_(&self) -> Result<Self::RwLockReadGuard<'_>, ()> {
fn try_read_(&self) -> Result<impl Deref<Target = Self::Item>, ()> {
self.try_read().map_err(|_| ())
}

async fn write_(&self) -> Self::RwLockWriteGuard<'_> {
self.write().await
}

fn blocking_write_(&self) -> Self::RwLockWriteGuard<'_> {
self.blocking_write()
}

fn try_write_(&self) -> Result<Self::RwLockWriteGuard<'_>, ()> {
self.try_write().map_err(|_| ())
}

fn get_mut_(&mut self) -> &mut Self::Item {
self.get_mut()
}
}

#[derive(Clone)]
Expand Down

0 comments on commit d9c86ed

Please sign in to comment.