question on generic property type #991
-
what trait shall I use to support generic type mod detail {
pub trait SupportedTypes {}
impl SupportedTypes for i64 {}
}
struct SlotInterface<T> {
last_value: Arc<Mutex<Option<T>>>,
log_key: String,
}
impl<T: detail::SupportedTypes + zbus::zvariant::Type + Sync + Send + 'static> SlotInterface<T> {
pub fn new(last_value: Arc<Mutex<Option<T>>>, key: &str) -> Self {
Self {
last_value,
log_key: key.to_string(),
}
}
}
#[interface(name = "is.centroid.Slot")]
impl<T: detail::SupportedTypes + zbus::zvariant::Type + Sync + Send + 'static> SlotInterface<T>
{
#[zbus(property)]
async fn value(&self) -> Result<T, zbus::fdo::Error> {
Ok(137)
// Ok("self.foo.clone()".to_string())
// let guard = self.last_value.lock().await;
// if let Some(value) = guard.clone() {
// Ok(value.into())
// } else {
// Err(zbus::fdo::Error::Failed("No value set".into()))
// }
}
} error[E0277]: the trait bound `zbus::zvariant::Structure<'_>: From<T>` is not satisfied
--> src/ipc.rs:175:1
|
175 | #[interface(name = "is.centroid.Slot")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<T>` is not implemented for `zbus::zvariant::Structure<'_>`, which is required by `zbus::zvariant::Value<'_>: From<T>` |
Beta Was this translation helpful? Give feedback.
Answered by
zeenix
Sep 16, 2024
Replies: 1 comment 9 replies
-
As the error message suggests and documented in the book, property methods require their return value to be transformable to |
Beta Was this translation helpful? Give feedback.
9 replies
Answer selected by
zeenix
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As the error message suggests and documented in the book, property methods require their return value to be transformable to
zvariant::Value
. I've never seen generics use with properties before so I can't guarantee it will work out of the box, but you need to addValue<'static>: From<T>
constraint or something like that (orTryFrom<T>
, I can't remember).