woboq / qmetaobject-rs

Integrate Qml and Rust by building the QMetaObject at compile time.
MIT License
647 stars 89 forks source link

the trait bound `String: Deref` is not satisfied #275

Open mingjunyang opened 2 years ago

mingjunyang commented 2 years ago

my rustc version: rustc 1.66.0-nightly (0da281b60 2022-10-27)

error[E0277]: the trait bound `String: Deref` is not satisfied

   --> qmetaobject/src/qmetatype.rs:438:87

    |

438 |     const CONVERSION_TO_STRING: Option<fn(&Self) -> QString> = Some(|s| QString::from(&*s as &str));

    |                                                                                       ^^^ the trait `~const Deref` is not implemented for `String`

    |

note: the trait `Deref` is implemented for `String`, but that implementation is not `const`

   --> qmetaobject/src/qmetatype.rs:438:87

    |

438 |     const CONVERSION_TO_STRING: Option<fn(&Self) -> QString> = Some(|s| QString::from(&*s as &str));

    |                                                                                       ^^^

For more information about this error, try `rustc --explain E0277`.

I try to fix the issuse, it's work.

diff --git a/qmetaobject/src/qmetatype.rs b/qmetaobject/src/qmetatype.rs

index 1d06100..f9bf9ce 100644

--- a/qmetaobject/src/qmetatype.rs

+++ b/qmetaobject/src/qmetatype.rs

@@ -435,7 +435,7 @@ where

 }

 impl QMetaType for String {

-    const CONVERSION_TO_STRING: Option<fn(&Self) -> QString> = Some(|s| QString::from(&*s as &str));

+    const CONVERSION_TO_STRING: Option<fn(&Self) -> QString> = Some(|s| QString::from(s.as_str()));

     const CONVERSION_FROM_STRING: Option<fn(&QString) -> Self> = Some(|s| s.to_string());

 }
ogoffart commented 2 years ago

Thanks for the report. Your code looks better than the original.

But this looks like a bug in the const engine of the nightly compiler. Why would this needs to be const?

ogoffart commented 2 years ago

Reported https://github.com/rust-lang/rust/issues/103677