首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用GAT来改进未来的失败:如何声明生命周期?

尝试使用GAT来改进未来的失败:如何声明生命周期?
EN

Stack Overflow用户
提问于 2022-11-15 06:19:25
回答 1查看 41关注 0票数 -1

版本:

铁锈-版本

每晚(95a3a7277 2022-10-31)

错误:

代码语言:javascript
复制
error: non-defining opaque type use in defining scope
  --> src/main.rs:22:9
   |
22 | /         async move {
23 | |             Some(self.name)
24 | |         }
   | |_________^ lifetime `'a` is part of concrete type but not used in parameter list of the `impl Trait` type alias

代码:

代码语言:javascript
复制
#![feature(associated_type_defaults)]
#![feature(generic_associated_types)]
#![feature(type_alias_impl_trait)]

use std::future::Future;

pub trait KvIterator {
    type NextFuture<'b>: Future<Output = Option<&'b [u8]>>
    where
        Self: 'b;
    fn next<'s>(&'s mut self) -> Self::NextFuture<'s>;
    //fn get_name<'s>(&'s self) -> &'s [u8];
}

struct Person<'a> {
    name: &'a [u8],
}

impl<'a> KvIterator for Person<'a> {
    type NextFuture<'b>
    where
        Self: 'b,
    = impl Future<Output = Option<&'b [u8]>>; //
    fn next<'s>(&'s mut self) -> Self::NextFuture<'s> {
        async move { Some(self.name) }
    }
    // fn get_name<'s>(&'s self) -> &'s [u8] {
    //     self.name
    // }
}

fn main() {
    // Person(Self) outlive 'a
    {
        let mut p = Person { name: b"" };
        {
            let name = vec![97, 98, 99];
            p.name = name.as_slice();
            // let name1 = p.next().await;

            //let name1 = p.get_name();
            //dbg!(name1);
        }
    }
}

游乐场:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7f48b1c310a1466d8427a16560fec7fd

EN

回答 1

Stack Overflow用户

发布于 2022-11-16 02:56:29

异步块代码试图捕获(将extern变量移动到闭包内部)使用'a,所以应该删除关键字'move‘声明生存期的self.name,如下所示:

代码语言:javascript
复制
async {
    Some(self.name)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74441311

复制
相关文章

相似问题

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