TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rusty Typestates – Starting Out

105 pointsby snowytreesover 3 years ago

12 comments

nllshover 3 years ago
I just read about typestates in Rust on Cliff Biffle&#x27;s blog here: <a href="http:&#x2F;&#x2F;cliffle.com&#x2F;blog&#x2F;rust-typestate&#x2F;#variation-state-types-that-contain-actual-state" rel="nofollow">http:&#x2F;&#x2F;cliffle.com&#x2F;blog&#x2F;rust-typestate&#x2F;#variation-state-type...</a><p>He covers a few additional parts of the typestate pattern, such as isolating data in specific states as well as sharing common implementations across a subset of states.<p>I&#x27;d also like to note that typestates also show up in functional programming under Indexed Monads, where a function might take a struct from an initial typestate, unwrap its data, and return a final(likely different) typestate. You can search Indexed Monad for more explanation there. If you work primarily in typescript you can find a production ready implementation of typestate programming here: <a href="https:&#x2F;&#x2F;github.com&#x2F;DenisFrezzato&#x2F;hyper-ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;DenisFrezzato&#x2F;hyper-ts</a>
Klasiasterover 3 years ago
Since this often matters when implementing protocols, there is research about session types and verification. Here is a Rust library for session types: <a href="https:&#x2F;&#x2F;github.com&#x2F;Munksgaard&#x2F;session-types" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Munksgaard&#x2F;session-types</a><p>I believe both have some overlap.
评论 #29436865 未加载
thechaoover 3 years ago
My gut feeling is that the combination of typestates and stackless coroutines (really: CSP) would make for a kick-ass driver design. It&#x27;s pretty solid for the toys I&#x27;ve designed for OpenGLES-2-class drivers. My gut feeling is that a lot of the command-buffer shenanigans in Vulkan, Metal, and DX45035+ come from trying to make reasoning about driver development tractable, while also not making the users&#x27; lives awful.
ohaziover 3 years ago
In Drone&lt;Hovering&gt;, shouldn&#x27;t land be:<p><pre><code> fn land(self) -&gt; Drone&lt;Idle&gt; { Drone::&lt;Idle&gt;::from(self) } </code></pre> instead of:<p><pre><code> fn land(self) -&gt; Drone&lt;Idle&gt; { Drone::&lt;Idle&gt;::new() } </code></pre> Otherwise the position assertions in drone_flies fail, because it&#x27;s looking at the position of a brand new Drone&lt;Idle&gt;, rather than one based on the position you just flew to.<p>Edit:<p>Yes it should. [1]<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;rustype&#x2F;drone&#x2F;blob&#x2F;main&#x2F;src&#x2F;drone.rs#L53" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rustype&#x2F;drone&#x2F;blob&#x2F;main&#x2F;src&#x2F;drone.rs#L53</a>
afranchukover 3 years ago
Typestates are an excellent way to provide stronger compile time guarantees, and I wish I saw them more in the wild (I have used them myself when appropriate, though I was unaware of the terminology).<p>However, the article mentions:<p>The attentive reader may have noticed that the consumption and conversion of self into other types implies the values are moved (and in some cases, copied) around.<p>This is untrue; those moves will definitely be optimized away and the typestates will end up being zero cost.
评论 #29438753 未加载
FranchuFranchuover 3 years ago
I wonder if it would be a feasible to have some sort of internal compile-time variable in each binding that could make compile-time states not have to use hacky methods like this one.<p>Essentially, instead of the compile-time value attached to a binding only being a type (with generics, optionally), it could also have a regular Rust value attached to it.
评论 #29438217 未加载
ajkjkover 3 years ago
This is the feature I most want in every language I use. The main usecase is having &#x27;initialized&#x27; &#x2F; &#x27;ready&#x27; &#x2F; &#x27;destroying&#x27; states for objects, which tends to be useful in all kinds of gluecode in lots of settings.
adavover 3 years ago
This approach would be a great addition to the AWS CDK. I was trying that out recently and, without constantly referring back to the AWS console or to the docs, it feels like flying blind.
lliamanderover 3 years ago
Note that you can do something like Typestates in Java using static inner classes that have no public constructors, though undoubtedly there are advantages to having first class language support. Behold:<p><pre><code> Scanner.OpenScanner sOpen = Scanner.open(&#x2F;*some source*&#x2F;); &#x2F;&#x2F;do some processing Scanner.ClosedScanner sClosed = sOpen.close(); sClosed.nextLine() &#x2F;&#x2F;type error </code></pre> I sometimes do this in combination with the Builder pattern. This &quot;typesafe builder&quot; allows for a fluent API that guides the user as to its use.<p>The point is, you can still use types to represent states.
评论 #29436177 未加载
评论 #29436174 未加载
babyover 3 years ago
I think they should have started with the poor man typestate, which is to create a type per state (can use wrapper types if needed)
brunoqcover 3 years ago
I wonder how it improved since the last article.
solmagover 3 years ago
I believe there is a library that can generate type-state machines for you in Rust, ie. you don&#x27;t have to code it by hand.
评论 #29443329 未加载