Skip to content

Commit

Permalink
try_map_guardからT: 'staticを外す
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Sep 18, 2024
1 parent 752aedb commit 74a482d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
19 changes: 10 additions & 9 deletions crates/voicevox_core/src/__internal/interop/raii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ pub enum MaybeClosed<T> {

// [`mapped_lock_guards`]のようなことをやるためのユーティリティ。
//
// `T: 'static`が入っているのは、`T` outlives `G`の関係にすることによりouroborosに突っ込んでSafe
// Rustで記述しきるため。
//
// [`mapped_lock_guards`]: https://github.com/rust-lang/rust/issues/117108
pub fn try_map_guard<'a, G, F, T, E>(guard: G, f: F) -> Result<impl Deref<Target = T> + 'a, E>
pub fn try_map_guard<'lock, 'target, G, F, T, E>(
guard: G,
f: F,
) -> Result<impl Deref<Target = T> + 'lock, E>
where
G: 'a,
G: 'lock,
'target: 'lock,
F: FnOnce(&G) -> Result<&T, E>,
T: 'static,
T: 'target,
{
return MappedLockTryBuilder {
guard,
Expand All @@ -27,16 +28,16 @@ where
.try_build();

#[self_referencing]
struct MappedLock<'a, G: 'a, T: 'static> {
struct MappedLock<'lock, 'target, G: 'lock, T: 'target> {
guard: G,

#[borrows(guard)]
content: &'this T,

marker: PhantomData<&'a ()>,
marker: PhantomData<&'lock &'target ()>,
}

impl<'a, G: 'a, T: 'static> Deref for MappedLock<'a, G, T> {
impl<'lock, 'target: 'lock, G: 'lock, T: 'target> Deref for MappedLock<'lock, 'target, G, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
Expand Down
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 @@ -186,7 +186,7 @@ pub(crate) enum JavaApiError {

pub(crate) struct Closable<T: HasJavaClassIdent>(std::sync::RwLock<MaybeClosed<T>>);

impl<T: HasJavaClassIdent + 'static> Closable<T> {
impl<T: HasJavaClassIdent> Closable<T> {
pub(crate) fn new(content: T) -> Self {
Self(MaybeClosed::Open(content).into())
}
Expand Down
32 changes: 17 additions & 15 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ exceptions! {
InvalidWordError: PyValueError;
}

struct Closable<T: 'static, C: PyTypeInfo, A: Async> {
struct Closable<T, C: PyTypeInfo, A: Async> {
content: A::RwLock<MaybeClosed<T>>,
marker: PhantomData<(C, A)>,
}

impl<T: 'static, C: PyTypeInfo, A: Async> Closable<T, C, A> {
impl<T, C: PyTypeInfo, A: Async> Closable<T, C, A> {
fn new(content: T) -> Self {
Self {
content: MaybeClosed::Open(content).into(),
Expand Down Expand Up @@ -140,21 +140,21 @@ impl<T: 'static, C: PyTypeInfo, A: Async> Closable<T, C, A> {
}
}

impl<T: 'static, C: PyTypeInfo> Closable<T, C, SingleTasked> {
impl<T, C: PyTypeInfo> Closable<T, C, SingleTasked> {
#[must_use = "中身は明示的に`drop`でdropすること"]
fn close(&self) -> Option<T> {
futures_lite::future::block_on(self.close_())
}
}

impl<T: 'static, C: PyTypeInfo> Closable<T, C, Tokio> {
impl<T, C: PyTypeInfo> Closable<T, C, Tokio> {
#[must_use = "中身は明示的に`drop`でdropすること"]
async fn close(&self) -> Option<T> {
self.close_().await
}
}

impl<T: 'static, C: PyTypeInfo, A: Async> Drop for Closable<T, C, A> {
impl<T, C: PyTypeInfo, A: Async> Drop for Closable<T, C, A> {
fn drop(&mut self) {
let content = mem::replace(self.content.get_mut_(), MaybeClosed::Closed);
if matches!(content, MaybeClosed::Open(_)) {
Expand All @@ -171,34 +171,36 @@ impl<T: 'static, C: PyTypeInfo, A: Async> Drop for Closable<T, C, A> {

trait Async {
const EXIT_METHOD: &str;
type RwLock<T: 'static>: RwLock<Item = T>;
type RwLock<T>: RwLock<Item = T>;
}

enum SingleTasked {}
enum Tokio {}

impl Async for SingleTasked {
const EXIT_METHOD: &str = "__exit__";
type RwLock<T: 'static> = std::sync::RwLock<T>;
type RwLock<T> = std::sync::RwLock<T>;
}

impl Async for Tokio {
const EXIT_METHOD: &str = "__aexit__";
type RwLock<T: 'static> = tokio::sync::RwLock<T>;
type RwLock<T> = tokio::sync::RwLock<T>;
}

trait RwLock: From<Self::Item> + 'static {
type Item: 'static;
type RwLockWriteGuard<'a>: DerefMut<Target = Self::Item>;
trait RwLock: From<Self::Item> {
type Item;
type RwLockWriteGuard<'a>: DerefMut<Target = Self::Item>
where
Self: 'a;
fn try_read_(&self) -> Result<impl Deref<Target = Self::Item>, ()>;
async fn 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> {
impl<T> RwLock for std::sync::RwLock<T> {
type Item = T;
type RwLockWriteGuard<'a> = std::sync::RwLockWriteGuard<'a, Self::Item>;
type RwLockWriteGuard<'a> = std::sync::RwLockWriteGuard<'a, Self::Item> where Self: 'a;

fn try_read_(&self) -> Result<impl Deref<Target = Self::Item>, ()> {
self.try_read().map_err(|e| match e {
Expand All @@ -223,9 +225,9 @@ impl<T: 'static> RwLock for std::sync::RwLock<T> {
}
}

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

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

0 comments on commit 74a482d

Please sign in to comment.