5 ms·
Yeah, and it's trivial in C++, what's your point? The example looks like it's been custom tailored to show how awesome Rust is, but then the post fails to even
by codr7 3y ago
Yeah, and it's trivial in C++, what's your point?
The example looks like it's been custom tailored to show how awesome Rust is, but then the post fails to even make the point (however contrived).
Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin<Input> or Pin<Output>.
- Warwolt 3y agoThere's no way in C++ to _statically_ guarantee that you don't simultaneously have the floating pin handle and the output pin handle. You cannot do it.
- codr7 3y agoI agree, I'm just waiting for someone to explain why I should care; what's the reason to not declare the pin statically as either or? I mean, I could easily come up with a contrived example that Rust can't do as well, but what's the point?
- detaro 3y ago> what's the reason to not declare the pin statically as either or? That for plenty uses cases, pins are not statically either or, but are switched at runtime. (and the article really fails IMHO by not showing that, because doing that while keeping compile-time checks is the hard part, and its limits would be really interesting)
- pjmlp 3y agoYou can do that by creating types for the pins, and using HKT via template metaprogramming when instantiating the classes that hold handles to the pins. It the implementation code pretty? Most likely not, but here we are discussing what is possible, not beauty.
- sophacles 3y ago> Yeah, and it's trivial in C++, what's your point? I see lots of assertions of that fact, but no examples. Care to link some actual code? > Why the hell would you want to change the direction of a pin anyway? Just declare it as Pin<Input> or Pin<Output>. i2c uses the same pin for sending and receiving and changes the mode of the pin for the purposes of communication with peripherals.
- IshKebab 3y agoIgnoring the fact that nobody is bit bashing I2C (there are other protocols that you would bit bash), you don't normally change the mode in that case either. You set it to an output with drive-low and float-high. It's essentially an InputOutput mode.
- sophacles 3y agoI'm sure you mean "nobody" not nobody, otherwise this doesn't exist: https://github.com/bitbank2/BitBang_I2C https://github.com/bitbank2/BitBang_I2C (and plenty of others, this was just the first github link google gave me). There's quite a few articles suggesting to bit bang i2c on tiny MCUs also for various memory/code size/etc reasons. I suspect trivial to do in C++ really should be "trivial" too.
- IshKebab 3y agoYes in that context it means approximately nobody. In English it's normal to not speak 100% literally. Otherwise words like "nobody" and "everyone" become useless because there's always that one guy. The degree of literalness depends on the context, e.g. "everyone hates brutalist architecture" means maybe 90% of people hate it, while "everybody goes through puberty" means more like 99.99%. I'm guessing you're not a native English speaker - surely it is the same in other languages?
- sophacles 3y agoI'm being pedantic since you consistently refuse to back your claims with evidence. I assume you are going to keep deflecting and picking on nonsense instead of just providing the trivial code example. I'm sure you can do it, it's trivial right?
- IshKebab 3y agoDo what? This typestate pattern in C++? Of course you can. It's almost identical to the Rust code. The only downside is that C++'s moves leave the moved-from object accessible but invalid while the Rust compiler will not let you access them. Otherwise it's the same. If you just Google "C++ typestate" the first result is an HTTP library that uses this technique (plus a load of complex template stuff, but that isn't required). But I guess you've seen that already so maybe that's not what you're asking for?