gpt4 book ai didi

rust - 引入魔术线后第二个可变借用错误消失

转载 作者:行者123 更新时间:2023-12-03 11:26:04 25 4
gpt4 key购买 nike

这是我的结构(为了完整起见):

struct Client<'a, T>
where
T: AsyncRead + AsyncWrite + Unpin
{
socket: T,
stage: u8,
n: usize,
buf: &'a mut [u8],
}

然后我为该结构实现了一个Future,但是通过let me = &mut *self;<将self更改为me/。这编译得很好:

impl<'a, T> Future for Client<'a, T>
where
T: AsyncRead + AsyncWrite + Unpin
{
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {

let me = &mut *self; // <<<<<<<<<< this fixes it, how?!

while me.stage == 1 {
let self_n = me.n;
let mut rb = ReadBuf::new(&mut me.buf[..self_n]);
let n = ready!(Pin::new(&mut me.socket).poll_read(cx, &mut rb));
}
Poll::Pending
}
}

但是,如果我删除该转换,它将触发借用检查器错误:

impl<'a, T> Future for Client<'a, T>
where
T: AsyncRead + AsyncWrite + Unpin
{
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {

// magic line removed

while self.stage == 1 {
let self_n = self.n;
let mut rb = ReadBuf::new(&mut self.buf[..self_n]);
let n = ready!(Pin::new(&mut self.socket).poll_read(cx, &mut rb));
}
Poll::Pending
}
}
error[E0499]: cannot borrow `self` as mutable more than once at a time
--> z.rs:y:x
|
84 | let mut rb = ReadBuf::new(&mut self.buf[..RES.len() - self_n]);
| ---- first mutable borrow occurs here
85 | let n = ready!(Pin::new(&mut self.socket).poll_read(cx, &mut rb));
| ^^^^ ------- first borrow later used here
| |
| second mutable borrow occurs here

编译它的 let me = &mut *self; 是什么意思?

最佳答案

this thread 所示, 可变地借用 Pin 的一个元素需要使用其 DerefMut实现,它可变地借用了整个 Pin .因此,您不能可变地借用 2 个字段,即使它们是不相交的。线路let me = &mut *self (或者 let me = &mut self )使用 PinDerefMut实现生成 &mut Self来自 Pin<&mut Self> .它只会这样做一次,然后通过那个可变借用借用两个不相交的字段,这不会导致错误。

关于rust - 引入魔术线后第二个可变借用错误消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64733802/

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