-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improve: rework `VoiceModel` * diffを抑える工夫 * Minor refactor * `.ref_map(…)` → `.each_ref().map(…)` * `collect_results` → `collect`, `collect_future_results` → `join` * `join`から`Result`の`collect`を分離 * fixup! diffを抑える工夫 * `blocking`版はblockingクレートに依存しない * async_zip v0.0.17に備える * `SmolBlocking` → `BlockingThreadPool` * Minor refactor * `futures_lite::future::block_on`を`.block_on()`として使えるようにする * `join` → `join_all` #830 (comment) * `find_index` → `find_entry_index` #830 (comment) * "join"しない * Minor refactor * `crate::asyncs`にdoc #830 (comment) * `Unstoppable` → `SingleTasked` #830 (comment) https://chatgpt.com/share/cdae540e-5751-43a5-a1fb-ac1f17d6a1b8
- Loading branch information
Showing
16 changed files
with
726 additions
and
340 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! 非同期操作の実装の切り替えを行う。 | ||
//! | ||
//! 「[ブロッキング版API]」と「[非同期版API]」との違いはここに集約される | ||
//! …予定。現在は[`crate::voice_model`]のみで利用している。 | ||
//! | ||
//! # Motivation | ||
//! | ||
//! [blocking]クレートで駆動する非同期処理はランタイムが無くても動作する。そのため非同期版APIを | ||
//! もとにブロッキング版APIを構成することはできる。しかし将来WASMビルドすることを考えると、スレッド | ||
//! がまともに扱えないため機能しなくなってしまう。そのためWASM化を見越したブロッキング版APIのため | ||
//! に[`SingleTasked`]を用意している。 | ||
//! | ||
//! [ブロッキング版API]: crate::blocking | ||
//! [非同期版API]: crate::tokio | ||
//! [blocking]: https://docs.rs/crate/blocking | ||
|
||
use std::{ | ||
io::{self, Read as _, Seek as _, SeekFrom}, | ||
path::Path, | ||
pin::Pin, | ||
task::{self, Poll}, | ||
}; | ||
|
||
use futures_io::{AsyncRead, AsyncSeek}; | ||
|
||
pub(crate) trait Async: 'static { | ||
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin>; | ||
} | ||
|
||
/// エグゼキュータが非同期タスクの並行実行をしないことを仮定する、[`Async`]の実装。 | ||
/// | ||
/// [ブロッキング版API]用。 | ||
/// | ||
/// # Performance | ||
/// | ||
/// `async`の中でブロッキング操作を直接行う。そのためTokioやasync-stdのような通常の非同期ランタイム | ||
/// 上で動くべきではない。 | ||
/// | ||
/// [ブロッキング版API]: crate::blocking | ||
pub(crate) enum SingleTasked {} | ||
|
||
impl Async for SingleTasked { | ||
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin> { | ||
return std::fs::File::open(path).map(BlockingFile); | ||
|
||
struct BlockingFile(std::fs::File); | ||
|
||
impl AsyncRead for BlockingFile { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
_: &mut task::Context<'_>, | ||
buf: &mut [u8], | ||
) -> Poll<io::Result<usize>> { | ||
Poll::Ready(self.0.read(buf)) | ||
} | ||
} | ||
|
||
impl AsyncSeek for BlockingFile { | ||
fn poll_seek( | ||
mut self: Pin<&mut Self>, | ||
_: &mut task::Context<'_>, | ||
pos: SeekFrom, | ||
) -> Poll<io::Result<u64>> { | ||
Poll::Ready(self.0.seek(pos)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// [blocking]クレートで駆動する[`Async`]の実装。 | ||
/// | ||
/// [非同期版API]用。 | ||
/// | ||
/// [blocking]: https://docs.rs/crate/blocking | ||
/// [非同期版API]: crate::tokio | ||
pub(crate) enum BlockingThreadPool {} | ||
|
||
impl Async for BlockingThreadPool { | ||
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin> { | ||
async_fs::File::open(path).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::future::Future; | ||
|
||
use easy_ext::ext; | ||
|
||
/// `futures_lite::future::block_on`を、[pollster]のように`.block_on()`という形で使えるようにする。 | ||
/// | ||
/// [pollster]: https://docs.rs/crate/pollster | ||
#[ext(FutureExt)] | ||
impl<F: Future> F { | ||
pub(crate) fn block_on(self) -> Self::Output | ||
where | ||
Self: Sized, | ||
{ | ||
futures_lite::future::block_on(self) | ||
} | ||
} |
Oops, something went wrong.