gpt4 book ai didi

rust - 四处移动对象然后调用消耗对象自身的方法会产生 "cannot move out of borrowed content"

转载 作者:行者123 更新时间:2023-11-29 08:30:47 31 4
gpt4 key购买 nike

我知道有 1000 个问题,但我读到的似乎没有一个适合这个问题。

我正在做的是,在某个函数中我正在创建一个 RusotoFuture(来自 rusoto_s3 crate ):

fn execute(&mut self, s3: &S3Client) -> (i64, RusotoFuture<GetObjectOutput, GetObjectError>) {
...
let dl = s3.get_object(GetObjectRequest{
...
..Default::default()
});
return (sz, dl);

我返回这个对象,并存储它:

fn handle_op(&mut self, input: u64) {
...
let (sz, dl) = op.execute( &self.s3.as_ref().unwrap() );
self.pending = Some(dl);

稍后我想从 handle_op 函数调用此对象的同步,问题是签名消耗了自身(https://rusoto.github.io/rusoto/rusoto_core/struct.RusotoFuture.html#method.sync):

pub fn sync(self) -> RusotoResult<T, E>

并且 handle_op 使用 &mut self,所以我没有 RusotoFuture 的所有权。

fn execute(&mut self, req: &mut RusotoFuture<GetObjectOutput, GetObjectError>) {
...
let result = req.sync().expect("could not download");

调用方式:

op.execute(self.pending.as_mut().unwrap() );

错误:

let result = req.sync().expect("could not head");
^^^ cannot move out of borrowed content

我怎样才能做到这一点?无论如何我可以强制一些不安全的机制来获得它的所有权吗?我也在看 Box,但我遇到了同样的错误。不过,我可能没有正确使用它。

最佳答案

签名会消耗自身,因为在 Future 解析后保留它没有多大意义。所以你不应该在引用上调用同步。但是从你的问题中不清楚什么叫什么:

let result = req.sync().expect("could not download");
let result = req.sync().expect("could not head");
^^^ cannot move out of borrowed content

那些是同一行并且您更改了字符串还是它们是不同的行等?

但我仍然可以尝试回答。

let dl = s3.get_object(GetObjectRequest{
...
..Default::default()
});

get_object 返回的 RusotoFuture 不是引用,所以

fn handle_op(&mut self, input: u64) {
...
let (sz, dl) = op.execute( &self.s3.as_ref().unwrap() );
self.pending = Some(dl);

有一个消耗品 d1,即使它以 &mut self 作为参数。所以你应该改变执行功能

fn execute(&mut self, req: &mut RusotoFuture<GetObjectOutput, GetObjectError>) {...}

fn execute(&mut self, req: RusotoFuture<GetObjectOutput, GetObjectError>) {...}

然后称它为

op.execute( self.pending.take() );

在您的设置中是否有任何不可行的原因?

关于rust - 四处移动对象然后调用消耗对象自身的方法会产生 "cannot move out of borrowed content",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58568015/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com