我在rust中的库设计遇到了一些问题。我正在创建一个使用cpal的库。
我希望能够从外部调用start_stream
和stop_stream
,当你调用start_stream
时,我创建:
match(device.build_input_stream(... a config, a callback))
{
Ok(stream_in) => {
stream_in.play();
// stream_in goes out of memory instantly and the callback will never be called
}
Err(...
}
我基本上认为我可以创建一个全局状态(* 红色标记 *)来保存流,这样我就可以在将来停止它。
我试过这样的方法:
lazy_static! {
static ref STREAM_IN: Arc<Mutex<Option<Stream>>> = None;
}
// ... later when creating the stream I could save it here.
但这并不符合:
error[E0277]: `*mut ()` cannot be sent between threads safely
--> src/api.rs:66:1
|
66 | / lazy_static! {
67 | | static ref STREAM_IN: Arc<Mutex<Option<Stream>>> = None;
68 | | }
| |_^ `*mut ()` cannot be sent between threads safely
|
= help: within `Option<cpal::Stream>`, the trait `Send` is not implemented for `*mut ()`
= note: required because it appears within the type `PhantomData<*mut ()>`
= note: required because it appears within the type `cpal::platform::NotSendSyncAcrossAllPlatforms`
= note: required because it appears within the type `cpal::Stream`
= note: required because it appears within the type `Option<cpal::Stream>`
= note: required for `Mutex<Option<cpal::Stream>>` to implement `std::marker::Sync`
= note: 1 redundant requirement hidden
= note: required for `Arc<Mutex<Option<cpal::Stream>>>` to implement `std::marker::Sync`
note: required by a bound in `Lazy`
--> /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-1.4.0/src/inline_lazy.rs:19:20
|
19 | pub struct Lazy<T: Sync>(Cell<Option<T>>, Once);
| ^^^^ required by this bound in `Lazy`
= note: this error originates in the macro `__lazy_static_create` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `(dyn FnMut() + 'static)` cannot be sent between threads safely
--> src/api.rs:66:1
|
66 | / lazy_static! {
67 | | static ref STREAM_IN: Arc<Mutex<Option<Stream>>> = None;
68 | | }
| |_^ `(dyn FnMut() + 'static)` cannot be sent between threads safely
|
谁能告诉我做这件事更好的方法?
1条答案
按热度按时间gk7wooem1#
我创建了一个拥有音频流的流。通信是通过通道完成的。
比如: