The Builder Pattern and Typestate Programming - Stefan Baumgartner - Rust Linz January 2023

  Рет қаралды 15,074

Rust

Rust

Күн бұрын

Almost 30 years ago, the Gang of Four wrote about software design patterns. They were highly influential, and over the course of decades, the proposed patterns have been cited, reused, misused, thrown away, and found again, over and over. One of those patterns -- the builder pattern -- gained some popularity in Rust's libraries. Let's take a look at what does, why the builder pattern is such a great fit for Rust, and how we can use some of the unique features of Rust's type system to make them even better.
--
Stefan Baumgartner works at Dynatrace. He writes for Manning, Smashing Magazine and A List Apart and made the Kessel run in less than 12 parsecs.
In his spare time, he organizes ScriptConf and DevOne in Linz, and co-hosts the German language Working Draft podcast. Stefan enjoys Italian food, Belgian beer and British vinyl records.

Пікірлер: 20
@chillyvanilly6352
@chillyvanilly6352 9 ай бұрын
18:45 => Typestate Programming "chapter"
@erlonpb
@erlonpb 7 ай бұрын
the code demonstration at the end with neovim (nice setup!) really nailed it! Things became very clear
@Lantalia
@Lantalia 9 ай бұрын
Ahh yes, the Design Patterns book
@nirmalyasengupta6883
@nirmalyasengupta6883 9 ай бұрын
Having done quite some FSM-based programming, I completely understand and appreciate the value of Typestate! While getting it correct requires time and effort ( _con_ , some may say ), prevention of wrong APIs being called by the compiler, carries immense value ( the obvious _pro_ ). Thank you for a concise presentation, Stefan!
@kieransweeden
@kieransweeden 4 ай бұрын
This was really interesting! It was especially satisfying seeing the types change to reflect state based on previous calls. Thank you!
@axisaligned9799
@axisaligned9799 9 ай бұрын
small typo at 9:50: .uri("..."); .header("...", "..."); there shouldnt be a semicolon after the .uri()
@billyean
@billyean 5 ай бұрын
What if the build is available when set mutiple things need to be set, I guess you might have to use a bitset to decide if all things are set or not? Is there any better way?
@user-uk6th6pm2j
@user-uk6th6pm2j 3 ай бұрын
Wow! So compiler will not let you forget to pass a workload to construct a worker!
@MrYevelnad
@MrYevelnad 8 ай бұрын
This is really confusing at first but very insightful at the end.
@OliverUnderTheMoon
@OliverUnderTheMoon 8 ай бұрын
Thanks for the vid. I'd be curious if anyone might remark on these things: * I remember a criticism of "patterns" is that may indicate a deficit in language constructs, or maybe the wrong tool for the job -- it makes me wonder how else this problem could be solved, e.g. with some kind of imperative programming. * Could this pattern heighten the stack in an undesirable way? * Is this what the Warp library is doing with it's Boxy filter chains?
@cramhead
@cramhead 9 ай бұрын
Thanks for sharing this insight using typestate with the builder pattern. It sends very useful. One thing I wonder about is how would this scale when multiple required properties need to be set. I have to expect that either the required properties would have to be set in a specific order or that there would be an explosion of possible typestates. Is there another elegant solution that would deal well with that scenario?
@peter9477
@peter9477 9 ай бұрын
If multiple properties are *required* then I think (maybe) you could simply keep the final state as a separate typestate. I don't think the intermediate (incomplete) states are so important here, but I am not adept at using the typestate pattern yet so maybe I'm wildly off-base.
@ChronosWS
@ChronosWS 9 ай бұрын
The other variant of this I have seen uses PhantomData and a typestate struct like here, rather than using the typestate as the type of the missing data. I think I like the version presented here, which does not need PhantomData to implement. I wonder if there is some more subtle difference?
@boenrobot
@boenrobot 9 ай бұрын
I wonder how this generics approach works under the hood... specifically, does it do a copy on each state transition. I imagine maybe the PhantomData approach does not cause a copy on a state transition.
@TonyUnderscore
@TonyUnderscore 9 ай бұрын
I think the difference is that if you do use phantomdata, you can't store any data in whatever you are gentrifying over. In this example we say that workload is of type W which we then directly use in the build method with workload.into(). This means that it is not actually a zero sized type whilst if we used phantomdata, this would not be possible (as we can't store anything in it). I wouldn't consider myself anywhere near knowledgable in the language so please correct me if Im wrong :)
@TinBryn
@TinBryn 9 ай бұрын
@@boenrobot There really should be no difference between state transition using PhantomData or Unit structs, both are zero sized. The main difference is you can go from a unit struct to a non-zero sized type and add data to the new version, you can't do that with PhantomData. I would also add that storing the generic type directly also helps with type inference. I would argue that if you have a generic struct, and you intend for that generic type to be zero sized, you shouldn't use PhantomData and just store the type directly. One more thing is it should be the last field of a struct, this prevents it affecting alignment and padding of the rest of the fields, and allows unsizing.
@CristianGarcia
@CristianGarcia 9 ай бұрын
Feels like someone could create a macro for this that boils it down to a single python-like constructor.
@skytech2501
@skytech2501 4 ай бұрын
Enums instead of generics?!!
@linkernick5379
@linkernick5379 8 күн бұрын
Are named arguments abandoned forever? 😢
Rust at Aleph Alpha - Markus Klein - Rust Linz March 2023
22:00
When Jax'S Love For Pomni Is Prevented By Pomni'S Door 😂️
00:26
Master Golang with Interfaces
21:54
Kantan Coding
Рет қаралды 6 М.
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 156 М.
8 Design Patterns | Prime Reacts
22:10
ThePrimeTime
Рет қаралды 384 М.
Two Ways To Do Dynamic Dispatch
19:54
Logan Smith
Рет қаралды 70 М.
Regex Library in Rust from Scratch (Finite-State Machines)
2:14:17
Tsoding Daily
Рет қаралды 42 М.
Rust Programming: The Ultimate Builder Pattern Tutorial
20:22
Jeremy Chone
Рет қаралды 37 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 186 М.
Мечта Каждого Геймера
0:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 1,3 МЛН
Apple watch hidden camera
0:34
_vector_
Рет қаралды 61 МЛН
Iphone or nokia
0:15
rishton vines😇
Рет қаралды 1,7 МЛН
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 5 МЛН
🔥Идеальный чехол для iPhone! 📱 #apple #iphone
0:36
DC Fast 🏃‍♂️ Mobile 📱 Charger
0:42
Tech Official
Рет қаралды 482 М.