首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用柴油机连接和Gtk-rs事件?

如何使用柴油机连接和Gtk-rs事件?
EN

Stack Overflow用户
提问于 2020-04-22 18:34:04
回答 2查看 119关注 0票数 1

当按下gtk条目中的Enter键时,我尝试从数据库中获取一个对象。我的想法是创建一个柴油SQLite连接的主要功能,然后借用它,每次我需要它。

为此,我尝试使用一个非常简单的MVC。其想法是将连接传递给所有控制器并重用它。我知道生命在这里是有必要的。

代码语言:javascript
复制
pub struct Controller<'a> {
    pub connection: &'a SQLiteConnection,
    pub search_entry: SearchEntry,
    ....
}

impl<'a> Controller<'a> {

    pub fn new(conn: &'a SQLiteConnection) -> Self {
        Self {
            search_entry: SearchEntry::new(),
            connection: conn,
            ....
        }
    }

    pub fn init(&self) {
        self.search_entry.connect_activate(|x| {
            let need_it = diesel_table
            .filter(column_id.eq(&x.get_text().unwrap()))
            .first(self.connection)
            .unwrap();
        });
    }
}

在编译时,我得到了以下内容:

代码语言:javascript
复制
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/lib.rs:44:44
   |
44 |           self.search_entry.connect_activate(|x| {
   |  ____________________________________________^
45 | |             let need_it = diesel_table
46 | |                 .filter(column_id.eq(&x.get_text().unwrap()))
47 | |                 .first(self.connection)
48 | |                 .unwrap();
49 | |         });
   | |_________^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 43:5...
  --> src/lib.rs:43:5
   |
43 | /     pub fn init(&self) {
44 | |         let need_it = diesel_table
46 | |                 .filter(column_id.eq(&x.get_text().unwrap()))
47 | |                 .first(self.connection)
48 | |                 .unwrap();
49 | |         });
50 | |     }
   | |_____^
note: ...so that the types are compatible
  --> src/lib.rs:44:44
   |
44 |           self.search_entry.connect_activate(|x| {
   |  ____________________________________________^
45 | |             let need_it = diesel_table
46 | |                 .filter(column_id.eq(&x.get_text().unwrap()))
47 | |                 .first(self.connection)
48 | |                 .unwrap();
49 | |         });
   | |_________^
   = note: expected  `&&Controller<'_>`
              found  `&&Controller<'a>`
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/lib.rs:44:44: 49:10 self:&&Controller<'_>]` will meet its required lifetime bounds
  --> src/lib.rs:44:27
   |
44 |         self.search_entry.connect_activate(|x| {
   |                           ^^^^^^^^^^^^^^^^

这个错误是因为函数connect_activate有一个静态参数:fn connect_activate<F: Fn(&Self) + 'static>

我不希望每次用户使用返回连接的函数按Intro键时初始化连接。相反,我想借用一下这种联系。

这样做最有效的方法是什么?

非常感谢。我希望你能理解一切。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-23 12:26:55

我想你需要一个Rc<SQLiteConnection>

票数 1
EN

Stack Overflow用户

发布于 2020-04-24 10:00:02

除了OP提到的问题之外,这个问题还包含其他各种代码问题,因此我将根据上面提供的信息包括一个完整的工作示例。对于未来的问题:请确保您的示例是独立的,并且不包含问题中提到的其他编译错误。这将使其他人更容易重现你的问题并回答问题。

代码语言:javascript
复制
#[macro_use] extern crate diesel;

use diesel::prelude::*;

table! {
    diesel_table(id) {
        id -> Integer,
        column_id -> Text,
    }
}

pub struct Controller<'a> {
    pub connection: &'a SqliteConnection,
    pub search_entry: SearchEntry,
}

pub struct SearchEntry;

impl SearchEntry {
    fn new() -> Self {
        SearchEntry
    }

    fn connect_activate<'a, F: Fn(&Self) + 'a>(&self, f: F) {
        f(self)
    }

    fn get_text(&self) -> Option<&'static str> {
        Some("foo")
    }
}

impl<'a> Controller<'a> {

    pub fn new(conn: &'a SqliteConnection) -> Self {
        Self {
            search_entry: SearchEntry::new(),
            connection: conn,
        }
    }

    pub fn init(&self) {
        self.search_entry.connect_activate(|x| {
            use self::diesel_table::dsl::*;

            let need_it: (i32, String) = diesel_table
            .filter(column_id.eq(&x.get_text().unwrap()))
            .first(self.connection)
            .unwrap();
        });
    }
}

解决OP中描述的错误的唯一相关更改是对connect_activate的以下更改

代码语言:javascript
复制
- fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) {
+ fn connect_activate<'a, F: Fn(&Self) + 'a>(&self, f: F) {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61372564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档