如何使用 Rust 正确地子类化 glib?

How do I properly subclass with glib using Rust?

我正在学习 this 教程,因为我想创建一个 gstreamer 插件。我认为教程有点过时,但是 Cargo.toml 文件没有指定教程使用的是哪个版本。我在使用 glib 时遇到了一些问题,我希望对 glib 有更多经验的人可以帮助我。

教程提示我写以下几行。 我在评论中添加了我遇到的错误,以及我已经尝试过的事情:

mod rgb2gray/imp

#[derive(Default)]
pub struct Rgb2Gray {}

impl Rgb2Gray {}

// This macro doesn't seem to exist 
// I tried putting "glib::glib_object_subclass!();" inside the struct (???)
// after that I get: "the trait `glib::subclass::object::ObjectImpl` is not implemented for `rgb2gray::imp::Rgb2Gray`"
#[glib::object_subclass] 
impl ObjectSubclass for Rgb2Gray {
    const NAME: &'static str = "RsRgb2Gray";
    type Type = super::Rgb2Gray; // TYPE is not a variable in ObjectSubclass
    type ParentType = gst_base::BaseTransform;
}

mod rgb2gray

mod imp;

// Again, this macro doesn't seem to exist
// I tried glib::glib_wrapper
glib::wrapper! {
    // The following line doesn't accept ObjectSubclass, it does accept Boxed, or Object, or Shared.
    //  I tried Object, but then it wants more information in the angle brackets... And I'm not sure what kind of information.
    pub struct Rgb2Gray(ObjectSubclass<imp::Rgb2Gray>) @extends gst_base::BaseTransform, gst::Element, gst::Object;
}

// more code...

您是否将其与 https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/master/tutorial 处的代码进行了比较?那个构建得很好,所以我假设某个地方的代码和降价不同步。

从您上面写的错误来看,您使用的是最新版本的 glib 而不是 git 版本。请参阅其中的 Cargo.toml,了解如何 select git 版本。

如果您使用的是最新版本,则需要 0.6 分支中的 code/markdown:https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/0.6/tutorial