铁锈和 GTK+3。如何创建包含一些 glib::Value 类型元素的数组,这些元素可以转换为 &[&ToValue] 类型?

Rust & GTK+3. How to create array containing a few elements of type glib::Value which can be casted to &[&ToValue] type?

我正在使用 Rust 和 GTK+ 3 绑定(称为 Gtk-rs 或 rust-gnome)创建 GUI 应用程序。我想使用方法 insert_with_values

将一些数据插入 gtk::ListStore (GtkListStore)

工作示例,但只有一列: 如果我有一列,我可以插入数据:

let type_of_a_column = &[gtk::Type::U32];
let data_for_column_1 = (2 as u32).to_value();
let model_list_of_data = ListStore::new(type_of_a_column);
model_list_of_data.insert_with_values(None, &[0 as u32], &[&data_for_column_1]);

但是,如果我有更多的列,我不知道如何交出更多的数据。 我知道在内部它应该调用 gtk_list_store_insert_with_valuesv() 所以我可能需要一个数组或切片,但如果我做类似的事情:

想传递数组中的两个元素,不工作

let type_of_a_column = &[gtk::Type::U32, gtk::Type::I64];
let data_for_column_1 = (2 as u32).to_value();
let data_for_column_2 = (4 as i64).to_value();
let array_of_data = [&data_for_column_1, &data_for_column_2];
let model_list_of_data = ListStore::new(type_of_a_column);
model_list_of_data.insert_with_values(None, &[1 as u32], &[&array_of_data]);

它在编译过程中给我:

error: the trait bound [&glib::Value; 2]: glib::IsA<glib::Object> is not satisfied [E0277]

也不知道怎么克服

MCVE 不工作

main.rs

extern crate gtk;

use gtk::prelude::*;
use gtk::{Box, ListStore, Orientation, TreeView, Window, WindowType};

fn exit_app() -> gtk::prelude::Inhibit {
    // Stop the main loop.
    gtk::main_quit();
    // Let the default handler destroy the window.
    Inhibit(false)
}

fn main() {
    if gtk::init().is_err() {
        panic!("Failed to initialize GTK.");
    }

    println!("Version of GTK+3:\t{}.{}",
             gtk::get_major_version(),
             gtk::get_minor_version());
    let window = Window::new(WindowType::Toplevel);
    window.set_title("Exercises with GtkTreeView and GtkListStore");
    window.set_position(gtk::WindowPosition::Center);
    window.connect_delete_event(|_, _| exit_app());

    let box_container = Box::new(Orientation::Vertical, 0);
    let view_list = TreeView::new();

    let types_inside_columns = &[gtk::Type::U32, gtk::Type::I64];
    let data_in_column_1 = (2 as u32).to_value();
    let data_in_column_2 = (4 as i64).to_value();

    let array_of_data = [&data_in_column_1, &data_in_column_2];

    let model_list_of_data = ListStore::new(types_inside_columns);

    model_list_of_data.insert_with_values(Some(0), &[0 as u32, 1 as u32], &[&array_of_data]);

    view_list.set_model(Some(&model_list_of_data));
    box_container.pack_start(&view_list, false, false, 0);
    window.add(&box_container);

    window.show_all();
    gtk::main();
}

Cargo.toml

[package]
name = "test_of_gtk_3"
version = "0.0.1"
authors = ["wm_obsd"]

[dependencies.gtk]
version = "0.1.0"
features = ["v3_16"]

[[bin]]
name = "main"
path = "src/main.rs"

最终,你最有可能想写这样的东西:

model_list_of_data.insert_with_values(Some(0), &[0, 1], &[&2u32, &4i64]);

让我们稍微分解一下函数定义:

fn insert_with_values(&self,
                      position: Option<u32>,
                      columns: &[u32],
                      values: &[&ToValue])
                      -> TreeIter;

此函数采用对 ListStore 的不可变引用、可选位置、零个或多个整数列以及对特征 [=21] 的零个或多个 引用=].

哪些类型实现了 ToValue?文档说:

impl<T> ToValue for Option<T> where T: SetValueOptional
impl<T> ToValue for T where T: SetValue + ?Sized
impl ToValue for Value

看看 SetValue,我们可以看到它是为原始整数类型实现的。

由于 u32i64 实现了 SetValue,它们也实现了 ToValue。对数字的引用也可以隐式地强制转换为对特征的引用。

进行此更改后,出现 window,但是...

GTK的奥秘就在这里!