advance/async/pin-unpin #788
Replies: 36 comments 42 replies
-
好难,始终对pin理解的不太好 |
Beta Was this translation helpful? Give feedback.
-
图上的内存地址分别代表什么的地址,红色箭头的指向代表什么意思希望能标注或者注释下,看不太懂。因为我原先理解“移到”的意思是一个不能浅拷贝的变量值从绑定a变量换到绑定b变量,变量值内存地址是不变的,变的是b有个指向变量值的地址而且拥有所有权。而在这里貌似变量值的地址也变了,这样的话会涉及到重新分配堆内存,效率肯定低的,应该不会这么设计,所以想借助上面的图来理解值移到到底是移到的啥 |
Beta Was this translation helpful? Give feedback.
-
看了大量的文章好像都没有说清楚一个问题(或者说没有让我看明白):到底 pin 是 1)将 move 之后的 invalid 指针重新变成了 valid,还是 2)仅仅是阻止代码构建? 将值固定到栈上 的例子让我感觉实现的是 2),固定到堆上 的例子则有些莫名其妙,打印一下就完事了,不应该和 将值固定到栈上 的例子要对应以来吗,不 swap 一下啥的吗? 谢谢~~ |
Beta Was this translation helpful? Give feedback.
-
所以说 |
Beta Was this translation helpful? Give feedback.
-
我真看不懂,看了两遍 新手 |
Beta Was this translation helpful? Give feedback.
-
rust中Future是通过Generator实现的,看了下Generator生成的时候用了std::mem::replace来查看状态,但是参数命名是Pin的,为什么能调用std::mem::replace |
Beta Was this translation helpful? Give feedback.
-
真的晕了。。。rust的各种指针好难记 |
Beta Was this translation helpful? Give feedback.
-
但是实际运行后,却产生了下面的输出 |
Beta Was this translation helpful? Give feedback.
-
这个地方写错了 但是实际运行后,却产生了下面的输出: a: test1, b: test1
a: test1, b: test2 应改为 a: test2, b: test1
a: test1, b: test2 应该将第一个 a: |
Beta Was this translation helpful? Give feedback.
-
看了一下Pin的官方说明, 其实 Pin 这个结构体并不是让 Pin 中的值变的不可移动, 而是让你无法在不使用 unsafe 方法的前提下改动 Pin 中的值地址而已. 真正标志一个值是不是不可移动的只有 !Unpin 这个标志. 这就是为什么 Pin 只是一个结构体, 而 Unpin 是一个编译器自动实现的 trait 的原因. |
Beta Was this translation helpful? Give feedback.
-
被pin住的堆上数据就不会发生移动了吗 |
Beta Was this translation helpful? Give feedback.
-
已经麻了,这节。 |
Beta Was this translation helpful? Give feedback.
-
关于最后一节Future变为UnPin: |
Beta Was this translation helpful? Give feedback.
-
来自ChatGPT: 在通常情况下,Rust的所有权系统和借用检查器能够确保引用的有效性。通过这些机制,Rust确保在编译时检查引用是否有效,以避免悬垂引用或竞争条件等错误。 然而,在异步编程中,可能会出现一些特殊情况,这时需要使用
需要注意的是,Pin仅仅是为了异步编程引入的特殊情况下使用的,并不是在所有情况下都需要使用。在大多数情况下,Rust的所有权系统和借用检查器已经足够确保引用的有效性。只有在异步编程中需要在异步执行期间保持引用有效时才需要使用 |
Beta Was this translation helpful? Give feedback.
-
从某些角度来看,Box、Rc、Arc等智能指针确实都提供了类似于Pin的功能:它们都保证了对象在内存中的位置不会发生改变(只有智能指针本身可以移动)。 然而,将Pin定义为一个trait并让这些智能指针去实现它可能会引入额外的复杂性和可能的混乱。例如,Rc和Arc是允许多个共享所有权的,当最后一个指向该数据的智能指针被丢弃时,数据会被自动清理。但如果我们把Rc或Arc用在需要固定内存位置的场景(比如异步编程),可能会出现问题。因为此时可能还有其他地方的代码保留着对原始数据的引用,而清理操作会导致悬挂引用。 此外,Pin更多的是一种约束,它表示对象应该是被"固定"的,即不能被移动。它不只适用于Box、Rc和Arc,也适用于任何实现了Deref和DerefMut trait的类型,包括裸指针*mut T和引用&mut T。因此,将Pin定义为一个通用的包装器结构体是有意义的。gpt回答的,不知道有没有道理 🐶 |
Beta Was this translation helpful? Give feedback.
-
Pin不过是个在其生命周期内无法通过Pin调用get_mut来获取一个可变引用的手段 当你的函数参数是Pin 你又无法通过参数拿到可变引用 自然无法移动 |
Beta Was this translation helpful? Give feedback.
-
最后一段完全没有看懂呀 将固定住的 Future 变为 Unpin,尤其是这一句
明明是 |
Beta Was this translation helpful? Give feedback.
-
Pin唯一的作用就是在实现了!Unpin的情况下阻止你调用get_mut |
Beta Was this translation helpful? Give feedback.
-
好像有点懂了,总而言之,Pin的作用就是保证被其包裹的指针所指向的值不能被 改动 ,其实我觉得作者文中说的 移动 多少有点让人产生误解,移 这个字总有种挪动的感觉,但其实本质就是 改动 , |
Beta Was this translation helpful? Give feedback.
-
介绍为何需要pin的图是否使用该博客里的"Self-reference is unsafe"段落里的图会更好?感觉现在的解释有些不清晰 |
Beta Was this translation helpful? Give feedback.
-
完全看不懂了:( |
Beta Was this translation helpful? Give feedback.
-
输出
本身不难理解,互换后的俩value都打出来就好理解为什么要pin了,swap之后a的值互换,b指向的目标互换; |
Beta Was this translation helpful? Give feedback.
-
这个Pin,Box,Arc起名真的是一眼明了。实在不行就叫i j k多好 |
Beta Was this translation helpful? Give feedback.
-
是我漏看了吗,好像一直看到现在也没有讲到虚类型? |
Beta Was this translation helpful? Give feedback.
-
Test { data: "数据", rData: 0x25ed4ff510 }
|
Beta Was this translation helpful? Give feedback.
-
advance/async/pin-unpin
https://course.rs/advance/async/pin-unpin.html
Beta Was this translation helpful? Give feedback.
All reactions