Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Pin in Rust

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to understand Pin in Rust? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Related concepts

Pin this is a struct, the function is to point to the T in memory fixed, can not be moved. To put it bluntly, you can't get & mut T through safe code. Pin

Defined as follows: pub struct Pin

{pointer: P,} Unpin this is a trait, defined in std::marker. If a T: Unpin, it means that T can move safely after pin. In fact, you can get & mut T. Pub auto trait Unpin {}! Unpin rejects Unpin, and the double negation of! Unpin is pin. If a type contains PhantomPinned, then the type is! Unpin.

Pub struct PhantomPinned

# [stable (feature = "pin", since = "1.33.0")] impl! Unpin for PhantomPinned {} Pin

We only focus on the safe method here, focusing on the new method: impl Pin

{pub fn new (pointer: P)-> Pin

{unsafe {Pin::new_unchecked (pointer)} can see that only T: Unpin that P points to can new a Pin. T here is the instance that should be pin, but because T: Unpin actually the instance of T will not be pin. In other words, when T does not implement Unpin trait, T will be lived by the real pin. Since the Pin::new method requires T: Unpin, the usual way to create a pin instance of T that does not support Unpin is the Box::pin method, which is defined as follows: pub fn pin (x: t)-> Pin {(box x). Into ()} for example, the Node structure is customized and the following code generates the pin instance: let node_pined: Pin = Box::pin (Node::new ()); let movded_node_pined = node_pined When Node doesn't implement Unpin, you can't get & mut Node through the security methods of Pin, so you can't move Node instances. Note that the Node instance cannot be moved here. Node_pined is an Pin instance and can be moved. Of course, you can still get mut Node through Pin's unsafe method, and you can also move Node instances, but these unsafe operations are at the programmer's own risk. This is explained in detail in the related methods of Pin. Pin can be regarded as a structure of restricted pointer (Box or & mut T). In the case of T: Unpin, Pin and Box are similar. & mut T can be obtained directly through DerefMut. If T does not realize Unpin, Pin can only get & T through Deref, that is to say, T is occupied by pin. Pin this kind of self-abandoning martial arts method is strange, why should there be Pin? Although Box, Rc, Arc and other pointer types can also make instances fixed in heap, the safe methods of these pointers will expose & mut T, which will cause instances of T to be moved, such as through std::mem::swap methods, Option::take methods, Vec::set_len, Vec::resize methods, etc., these are all safe methods. What these methods have in common is the need for & mut Self, so as long as the & mut Self is not exposed, the goal of pin can be achieved. Why do I need pin? The reason for this is the need for Async/.Await asynchronous programming. Take a look at the following asynchronous programming code: let fut_one = / *. * /; let fut_two = / *. * /; async move {. Fut_one.await;... Fut_two.await;.} rustc will automatically generate code similar to the following when compiled, in which the AsyncFuture will be a self-referenced structure: / / The `Future` type generated by our `async {.} `blockstruct AsyncFuture {. Fut_one: FutOne, fut_two: FutTwo, state: State,}

/ / List of states our `async` block can be inenum State {AwaitingFutOne, AwaitingFutTwo, Done,}

Impl Future for AsyncFuture {type Output = ()

Fn poll (mut self: Pin, cx: & mut Context

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report