shakuクレートでMutexが付けられない
こんにちは。Rustでソフトウェア開発をしています。
DIコンテナーを導入して簡単にコンポーネントの依存関係を解決したいと思い、調べているとshaku
というクレートを見つけました。
サンプルコードを実行してみると問題なく動きました。
このArc
に直接dyn Logger
入れると動作するのを確認できたのですが、実用的ではないと感じます。
なぜならArc
はMutex
と併せて使うのが一般的で、オブジェクトを可変にできるようにしないと値が固定され変数として意味を成さないからです。
そこでMutex
をArc
に付与してMutex<dyn Logger>
を入れるようにして実行してみました。
該当のコード
rust
1use shaku::{module, Component, HasComponent, Interface}; 2use std::sync::{Arc, Mutex}; 3 4trait Logger: Interface { 5 fn log(&self, content: &str); 6} 7 8trait DateLogger: Interface { 9 fn log_date(&self); 10} 11 12#[derive(Component)] 13#[shaku(interface = Logger)] 14struct LoggerImpl; 15 16impl Logger for LoggerImpl { 17 fn log(&self, content: &str) { 18 println!("{}", content); 19 } 20} 21 22#[derive(Component)] 23#[shaku(interface = DateLogger)] 24struct DateLoggerImpl { 25 #[shaku(inject)] 26 logger: Arc<Mutex<dyn Logger>>, // => コンパイルエラー 27 #[shaku(default)] 28 today: String, 29 #[shaku(default)] 30 year: usize, 31} 32 33impl DateLogger for DateLoggerImpl { 34 fn log_date(&self) { 35 self.logger 36 .log(&format!("Today is {}, {}", self.today, self.year)); 37 } 38} 39 40module! { 41 MyModule { 42 components = [LoggerImpl, DateLoggerImpl], 43 providers = [] 44 } 45} 46 47fn main() { 48 let module = MyModule::builder() 49 .with_component_parameters::<DateLoggerImpl>(DateLoggerImplParameters { 50 today: "Jan 26".to_string(), 51 year: 2020, 52 }) 53 .build(); 54 55 let date_logger: &dyn DateLogger = module.resolve_ref(); 56 date_logger.log_date(); 57}
しかしそうするとエラーが出ます。
このエラーが解決できずに困っています。
試したこと
Logger
にSized
トレイトをつけてみるMutex<LoggerImpl>
にLogger
を実装してみる
しかしエラーが出ます。
もし解決方法をご存知の方がいらっしゃいましたら、恐縮ですが教えて頂けないでしょうか。
どうぞよろしくお願いいたします。
エラーメッセージ抜粋
console
1error[E0277]: the size for values of type `(dyn Logger + 'static)` cannot be known at compilation time 2 --> src/main.rs:22:10 3 | 422 | #[derive(Component)] 5 | ^^^^^^^^^ doesn't have a size known at compile-time 6 | 7 = help: within `Mutex<(dyn Logger + 'static)>`, the trait `Sized` is not implemented for `(dyn Logger + 'static)` 8 = note: required because it appears within the type `Mutex<(dyn Logger + 'static)>` 9 = note: required because of the requirements on the impl of `Interface` for `Mutex<(dyn Logger + 'static)>` 10note: required by a bound in `HasComponent` 11 --> /Users/a.kano/.cargo/registry/src/github.com-1ecc6299db9ec823/shaku-0.6.1/src/component.rs:71:27 12 | 1371 | pub trait HasComponent<I: Interface + ?Sized>: ModuleInterface { 14 | ^^^^^^^^^ required by this bound in `HasComponent` 15 = note: this error originates in the derive macro `Component` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyModule: HasComponent<Mutex<(dyn Logger + 'static)>>` is not satisfied --> src/main.rs:40:1 | 40 | / module! { 41 | | MyModule { 42 | | components = [LoggerImpl, DateLoggerImpl], 43 | | providers = [] 44 | | } 45 | | } | |_^ the trait `HasComponent<Mutex<(dyn Logger + 'static)>>` is not implemented for `MyModule` | = help: the following implementations were found: <MyModule as HasComponent<(dyn Logger + 'static)>> <MyModule as HasComponent<<DateLoggerImpl as shaku::Component<MyModule>>::Interface>> note: required because of the requirements on the impl of `shaku::Component<MyModule>` for `DateLoggerImpl` --> src/main.rs:22:10 | 22 | #[derive(Component)] | ^^^^^^^^^ 23 | #[shaku(interface = DateLogger)] 24 | struct DateLoggerImpl { | ^^^^^^^^^^^^^^ = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyModule: HasComponent<Mutex<(dyn Logger + 'static)>>` is not satisfied --> src/main.rs:40:1 | 40 | / module! { 41 | | MyModule { 42 | | components = [LoggerImpl, DateLoggerImpl], 43 | | providers = [] 44 | | } 45 | | } | |_^ the trait `HasComponent<Mutex<(dyn Logger + 'static)>>` is not implemented for `MyModule` | = help: the following implementations were found: <MyModule as HasComponent<(dyn Logger + 'static)>> <MyModule as HasComponent<<DateLoggerImpl as shaku::Component<MyModule>>::Interface>> note: required because of the requirements on the impl of `shaku::Component<MyModule>` for `DateLoggerImpl` --> src/main.rs:22:10 | 22 | #[derive(Component)] | ^^^^^^^^^ 23 | #[shaku(interface = DateLogger)] 24 | struct DateLoggerImpl { | ^^^^^^^^^^^^^^ note: required because it appears within the type `MyModule` --> src/main.rs:41:5 | 41 | MyModule { | ^^^^^^^^ note: required by a bound in `Module` --> /Users/a.kano/.cargo/registry/src/github.com-1ecc6299db9ec823/shaku-0.6.1/src/module/module_traits.rs:36:19 | 36 | pub trait Module: ModuleInterface { | ^^^^^^^^^^^^^^^ required by this bound in `Module` = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyModule: HasComponent<Mutex<(dyn Logger + 'static)>>` is not satisfied --> src/main.rs:40:1 | 40 | / module! { 41 | | MyModule { 42 | | components = [LoggerImpl, DateLoggerImpl], 43 | | providers = [] 44 | | } 45 | | } | |_^ the trait `HasComponent<Mutex<(dyn Logger + 'static)>>` is not implemented for `MyModule` | = help: the following implementations were found: <MyModule as HasComponent<(dyn Logger + 'static)>> <MyModule as HasComponent<<DateLoggerImpl as shaku::Component<MyModule>>::Interface>> note: required because of the requirements on the impl of `shaku::Component<MyModule>` for `DateLoggerImpl` --> src/main.rs:22:10 | 22 | #[derive(Component)] | ^^^^^^^^^ 23 | #[shaku(interface = DateLogger)] 24 | struct DateLoggerImpl { | ^^^^^^^^^^^^^^ note: required because it appears within the type `MyModule` --> src/main.rs:41:5 | 41 | MyModule { | ^^^^^^^^ note: required by a bound in `HasComponent` --> /Users/a.kano/.cargo/registry/src/github.com-1ecc6299db9ec823/shaku-0.6.1/src/component.rs:71:48 | 71 | pub trait HasComponent<I: Interface + ?Sized>: ModuleInterface { | ^^^^^^^^^^^^^^^ required by this bound in `HasComponent` = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info) For more information about this error, try `rustc --explain E0277`. error: could not compile `try_shaku_mutex` due to 4 previous errors Cargo-Process exited abnormally with code 101 at Fri Apr 29 14:44:53

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/30 09:54