I’m trying to create my own GObject for use in a ListView
pub mod custom_widget {
mod imp {
use std::cell::OnceCell;
use std::rc::Rc;
use gtk::glib;
use gtk::glib::{Properties};
use gtk::glib::property::{Property};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
#[derive(Properties)]
#[properties(wrapper_type = super::PointWidget)]
pub struct PointWidget_{
#[property(get, set)]
name: OnceCell<Rc<str>>,
}
#[glib::object_subclass]
impl ObjectSubclass for PointWidget_ {
const NAME: &'static str = "PointWidget";
type Type = super::PointWidget;
fn new() -> Self {
Self {name: OnceCell::new()}
}
}
#[glib::derived_properties]
impl ObjectImpl for PointWidget_ {}
}
use std::rc::Rc;
use gtk::glib;
use gtk::prelude::ObjectExt;
use gtk::subclass::prelude::ObjectImpl;
glib::wrapper! {
pub struct PointWidget(ObjectSubclass<imp::PointWidget_>);
}
impl PointWidget {
pub fn new(name: Rc<str>) -> Self {
let struct_: PointWidget = glib::Object::builder()
.property("name", name)
.build();
struct_
}
pub fn get_name(&self) -> &str {
self.property("name")
}
}
}
But when I try to compile the code, I get the following error
error[E0277]: the trait bound OnceCell<Rc<str>>: gtk4::glib::property::Property
is not satisfied
|
105 | #[property(get, set)]
| ^ the trait gtk4::glib::property::Property
is not implemented for OnceCell<Rc<str>>
|
= help: the trait gtk4::glib::property::Property
is implemented for OnceCell<T>
How can I create my own type that implements the gtk4::glib::property::Property trait?
Tried to implement my own structure
#[derive(ValueDelegate)]
pub struct MyStruct(Rc<str>);
#[derive(Properties)]
#[properties(wrapper_type = super::PointWidget)]
pub struct PointWidget_{
#[property(get, set)]
name: OnceCell<MyStruct>,
}
This resulted in the following error
error[E0277]: the trait bound Rc<str>: FromValue<'a>
is not satisfied
|
101 | #[derive(ValueDelegate)]
| ^^^^^^^^^^^^^ the trait FromValue<'a>
is not implemented for Rc<str>
|
= help: the following other types implement trait FromValue<'a>
:
<bool as FromValue<‘a>>
<char as FromValue<‘a>>
<i8 as FromValue<‘a>>
<i32 as FromValue<‘a>>
<i64 as FromValue<‘a>>
<u8 as FromValue<‘a>>
<u32 as FromValue<‘a>>
<u64 as FromValue<‘a>>
and 1572 others
= note: this error originates in the derive macro ValueDelegate
(in Nightly builds, run with -Z macro-backtrace for more info)
This works with Box, but I want to avoid unnecessary memory allocation because… I’m storing the data in a separate structure.
Any advice would be much appreciated.
user24856178 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.