top | item 35800477

(no title)

greenfield1 | 2 years ago

As I don't know Rust, honest question here, just to help me understand..

The `pins.d13.into_input();` part just returns an object/reference/... of a type that only has input-specific methods, while using `.into_output()` would do the same with only output-specific methods, correct? That's nice, but why couldn't you do the same thing in C++?

With a dependent type you could do `pins.d13.into(INPUT)` and get the input-specific type, but that seems to not be something Rust could do?

discuss

order

Warwolt|2 years ago

One unique aspect of Rust that's involved here is the ownership system. It's possible to create a library that will only ever allow one instance of `pins.d13` to exist, and once you call `into_input` that `d13` value gets consumed, so that you cannot use the `Pin<Input<Floating>, PB5>` value anymore, and is guaranteed to only have access to that pin via the `Pin<Output, PB5>` value.

In C++, there's no possibility of guaranteeing that a consumed value is not re-used in the same ergonomic way using the type system. I'd imagine you can still implement something like this in C++ with some kind of move semantics and asserts, but it will be a run time error and not a statically checked error.