5 ms·
You misunderstand; what confuses me is how 'static can mean the same thing in both of these contexts. T: 'static does not mean the references in T must exist f
by wokwokwok 6y ago
You misunderstand; what confuses me is how 'static can mean the same thing in both of these contexts.
T: 'static does not mean the references in T must exist for the lifetime of the application.
Ie. the lifetime constraint on T isn’t the same 'static from &'static where the reference must life for the entire lifetime of the application.
Types are static. T: 'static applies to instances at runtime.
These instances do not have and are not related to the 'static lifetime which is “the entire length of the application”.
I accept:
> Type: lifetime means that the type is constrained by the lifetime.
I don't accept the explanation:
> Because 'static means "the lifetime of the whole process". That means that the type is not constrained.
A does not follow from B.
If you want something to mean "A type cannot have references in it" then invent a 'noref lifetime.
'static in this context would mean that all members of T must be 'static, which means that all instances of T must be 'static.
It does not mean that.
I don’t understand why they have the same name; they are not the same thing; it is an example of failure to have orthogonality in the language design in my opinion.
- kaoD 6y agoSorry, I probably edited my comment while you were replying and added a couple links. Check misconception 2 here, I think it addresses your point. https://github.com/pretzelhammer/rust-blog/blob/master/posts/common-rust-lifetime-misconceptions.md#2-if-t-static-then-t-must-be-valid-for-the-entire-program https://github.com/pretzelhammer/rust-blog/blob/master/posts... EDIT to your edit: > 'static in this context would mean that all instances of T must be 'static. You mean in T: 'static? No. It means that any instance passed as type T must be bound by 'static and therefore could be held up to the end of 'static. This does not mean that they're allocated at compile time, it just so happens that static variables (allocated at compile time) are 'static but the causality is reversed. > If you want something to mean "A type cannot have references in it" then invent a 'noref lifetime. It can have references in it! As long as they're bound by 'static. Here's an example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=857389cd5251e037f5ba608566b89b04 https://play.rust-lang.org/?version=stable&mode=debug&editio...
- deleted 6y ago[deleted]
- wokwokwok 6y agoOh, I've read that. I just maintain that the word 'static is being overloaded here to mean multiple different things. 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. > It can have references in it! As long as they're bound by 'static. :) ...but it can also have values in it which are not 'static. So is T: 'static, or not? It's arbitrary semantics; ...but my take on it is: - IF you take "x is 'static" as meaning the "X is valid for entire lifetime of the application" then if: - x: &'static 'is static' and must be valid for the entire lifetime of the application. I would expect: - x: T + 'static 'is static' and must be valid for the entire lifetime of the application. I'm happy to agree that's not what it does mean, what I'm saying is that it is inconsistent for it not to mean that.
- AsyncAwait 6y ago> ...but it can also have values in it which are not 'static. I don't think that's right. It can only have references which live at least as long as 'static does (or longer, but 'static is the longest so...)
- kaoD 6y agoI think he means owned values (notice he didn't say "other references") But actually... owned values are bound by 'static!
- kaoD 6y ago> 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. Yes it is. T: 'static means T can live up to the end of the "lifetime of the entire application". > ...but it can also have values in it which are not 'static. I don't follow. Values are indeed bound by 'static. If they weren't we wouldn't be able to pass values to other threads (which can potentially last as long as our application's main thread). > 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. It is. Any owned instance without non-'static references inside can live up to the "lifetime of the entire application". You might drop them earlier if you wanted to, but you don't have to since it can live up to the "lifetime of the entire application" and therefore can be passed for example into a thread that can hold the value up to the end of the "lifetime of the entire application". Any owned value can be held indefinitely as long as the program is running. I'm taking a guess here: you mean that values' lifetimes can be constrained (I guess you mean by dropping the actual value). But it's the owner the one that ended it earlier, not the caller (where 'static applied). You will never be able to have an owned value with a lifetime shorter than 'static without dropping it and, if you drop it, you cannot pass it anywhere. Hence why any owned type that is passed is, by definition, bound by 'static. > ...but my take on it is: > - IF you take "x is 'static" as meaning the "X is valid for entire lifetime of the application" > then if: > - x: &'static 'is static' and must be valid for the entire lifetime of the application. > I would expect: > - x: T + 'static 'is static' and must be valid for the entire lifetime of the application. Your expectations are correct and that's what it is. Just replace "must be valid" with "can live up to". That's why you need to pass T: 'static to threads, because a separate thread needs something to hold up to the end of the application since a thread can potentially never end. https://doc.rust-lang.org/std/thread/fn.spawn.html https://doc.rust-lang.org/std/thread/fn.spawn.html
- AsyncAwait 6y ago> T: 'static does not mean the references in T must exist for the lifetime of the application. It means that T does not have non static references, so it is not a requirement that it lives for the lifetime of the application but can do so.
- GolDDranks 6y agoYou seem to misunderstand what type : lifetime means. > 'static in this context would mean that all members of T must be 'static, which means that all instances of T must be 'static. Your "which means that" isn't true. It doesn't mean that. The syntax type : lifetime indicates that the values of the type MUST BE ABLE to outlive the lifetime. It doesn't mean that they NEED to outlive the lifetime. This means, for example, i32 : 'static even in the case doesn't live the whole 'static lifetime. It COULD live, though, if the author of the code allocated it statically.