Rust Programming: TypeState Builder Pattern Explained

preview_player
Показать описание
Rust Programming Tutorial for the TypeState builder pattern. We will also learn how to use PhantomData as one of the states.

Some Notes:

1) PhantomData are erased by the compiler, meaning they do not exist at "runtime" but allow code to use them to leverage the compiler's type system.

2) As commented by 'Harold Ruiter,' the 'build()' method has become infallible, meaning it cannot fail, and therefore, it can just return 'Request', rather than Result of Request.

3) The TypeState pattern does not have to be used only with the builder pattern.

4) As mentioned in the video, generics increase the binary size and compile time. While, more often than not, generics' value outweighs those costs, there is still a point of diminishing return if generics are overused.

5) To follow the #4 point, in this tutorial, we showed the multi states pattern with URL and Method, but designing a builder with too many states, for each property, for example, would be overkill and have counter-productive side effects.

6) In theory, the pure definition of the TypeState pattern assumes the same type is returned, which is technically not feasible in Rust, as Generic creates a different type. However, from a practical Rust perspective, this is a good representation of the TypeState pattern.

Jeremy Chone:

Other popular Rust Programming videos:

Playlists:

Other notes:

- Tool used to do the green lines. ScreenBrush on Mac App Store (Gromit seems to be the equivalent on Linux)
- Edited with Davinci Resolve.
Рекомендации по теме
Комментарии
Автор

Doesn't build() become infallible? You should be able to omit Result<T> and just make it return T

HaroldR
Автор

Nice one, I'm slowly getting used to the enormous number of generic types in Rust. One downside I see is that the compile time errors aren't as clear as the runtime errors. "method 'build' not found in RequestBuilder<NoUrl>" isn't the clearest way to communicate that the consumer hasn't called the url method. Do you know of ways to improve that?

flyaruu
Автор

I appreciate the amount of work you put into your videos.

hojjat
Автор

This, and the previous video, are excellent - thank you. I'd love to see more of these design-focused videos from you, please. There are many Rust beginner/syntax videos, but a video on this kind of topic, where the strengths of the language are used to build useful software, is quite rare.

meowsqueak
Автор

So, in effect, you have some kind of Design by Contract in compile time. You can design an API in a way that it is impossible to have structures in an inconsistent state and the only checks that will remain in runtime will be those that are impossible to know at compile time like validation checks and runtime errors which in Rust you are forced to handle anyway because they are behind Option or Result.

ankar
Автор

Exactly what I was thinking when i requested it. Appreciate the quick turnaround time!

seblund
Автор

Awesome! Really powerful the way you use generics and you are allowed to compose them independently

agustindiaz
Автор

Haven't got much time lately but whenever I see your video Jeremy its always delight. Happy new year. I know your channel is going to explode this year.

learnityourself
Автор

Excellent video! Thanks. I have been making sincere attempt to build all applications/components by being _Type-driven_ ! I have been reasonably successful in Scala and Java, as well! I am trying to get used to similar direction in thinking about the _Types_ in Rust too. This video explains the approach very lucidly! 👍

nirmalyasengupta
Автор

Thank you for taking the time to make these videos, they are very helpful.

jorgeosorio
Автор

Very interesting. It does make me wonder if this is something you’d even want to do since there’s a lot of boilerplate involved. Due to the number of datapoint wise moves, for bigger structures it could get annoying to add new fields. But it’s still really cool

KayOScode
Автор

Wow, Rust makes this pattern really elegant to implement compared to other languages like Java! Great video, I subbed :)

FreshLlamanade
Автор

Insightful and easily adaptable for my own projects, thanks!

michaelheinrich
Автор

That video was awesome. Brilliantly depicted the mechanism. Easy to follow. Love it

Автор

Very clear and very useful. Great video.

hojjat
Автор

This is the best typestate video I've seen, thank you!

rsalmei
Автор

makes me motivated to keep learning rust as the compile time checks are powerful.

el_carbonara
Автор

yessss this is MUCH better than the builder pattern!! thank you for this

irlshrek
Автор

Fantastic video, cleared all my confusion!

parthmittal
Автор

Nice pattern but if some values are mandatory why not have them as parameters to the new function?

FabianVilersBe