What is the difference between Send and 'static on the close in rust?

Consider these structures:

struct Promise1<T, Err> {
    on_resolve:Option<|promised:T|: Send>,
    on_reject:Option<|failed:Err|: Send>,
}

struct Promise2<T, Err> {
    on_resolve:Option<|promised:T|: 'static>,
    on_reject:Option<|failed:Err|: 'static>,
}

struct Promise3<'a, T, Err> {
    on_resolve:Option<|promised:T|: 'a>,
    on_reject:Option<|failed:Err|: 'a>,
}

      

What's the difference, what's the difference between them?

What is "Submit" as a constraint, and why do I no longer need to provide a lifetime when submitting a submission? What is this implicit lifetime that Send produces?

In particular, what is the difference between Send and 'static as closure constraints.

For example, this works:

let k:|int|:Send = |i:int| {};
let p:|int|:Send = |i:int| {};
Promise1 {
    on_resolve:Some(k),
    on_reject:Some(p)
};

      

But this is not the case:

let k = |i:int| {};
let p = |i:int| {};
Promise1 {
    on_resolve:Some(k),
    on_reject:Some(p)
};

      

Mistake:

error: mismatched types: expected `core::option::Option<'static |_|:Send>`, found 
`core::option::Option<|int|>` (expected bounds `Send`, found no bounds)

      

... however this works fine using Promise2 or Promise3 with "static" and "a" respectively.

+3


source to share


2 answers


Send

is the "view" http://doc.rust-lang.org/reference.html#built-in-traits

send: the ability to send tasks across boundaries.

'static

is a life span that is special: a life span that lasts for the entire program.



The link also contains another highlighting section on Send

: (we haven't got the optimal layout for the link yet) http://doc.rust-lang.org/reference.html#type-kinds

Send: Types of this kind can be safely sent between tasks. This type includes scalars, boxes, procs, and structured types containing only other types belonging to it. All submission types are "static".

So why can you use them interchangeably in certain situations.

+3


source


Why are we talking about life?

A closure includes an environment, which may or may not contain references to the current thread's stack. This environment has a lifetime limit, which indicates the maximum duration for which it is viable, and corresponds to the shortest lifetime of any embedded link that you specify with the appropriate notation.

  • if it contains a reference to the current thread stack, then the lifetime of the closure must not exceed the lifetime of the referenced link
  • if it contains a reference to a static resource and is not associated in this way with the thread stack, then the lifetime of the closure is unlimited.

'static

- the upper limit, since it does not give any limitation, and therefore is 'static

used to indicate the absence of any limitation on life expectancy. He neatly avoids the special case: there is always an attachment to life, it's just that sometimes it is "infinite."

What does it mean Send

?

The type Send

is applied to an object to indicate that it can be safely sent to another stream, such as through a pipe. Hence, it is assumed that the specified object does not refer to the current stack of the thread, since otherwise the stack must unwind, the object will contain dangling references!



Why Send

does it seem to mean 'static

?

  • If the object does not have a reference to the current stack, its binding to the lifetime 'static

  • Send

    implies that the object does not have a reference to the current stack

Thus, it is trivial to Send

imply that the object's lifespan is 'static

.

But...

There are actually safe ways to refer to another stack reference, such as in fork-join parallelism, where the referenced thread makes it wait for references to borrow threads. This is touched upon here and appears to require different types than Send

to define exactly how data can be exchanged safely.

+2


source







All Articles