Is this a correct implementation of Rust's Result enum for Arduino's C++ environment?
Hello Reddit!
Previously, I tried to recreate Rust's `Result` enum in C++ for the ESP32.
https://old.reddit.com/r/cpp/comments/1aifn5o/recreating_the_rust_result_enum_in_c_on_an_esp32/
But I have since realized that my old version kinda sucked!
For context, Arduino doesn't support most of the C++ standard library, but the ESP32 does.
What I really wanted was something you could just paste into the Arduino IDE and it would just work.
Now that I've rewritten it, it's possible!
However, I'm kind of sad to admit that I used AI (Google Gemini) to code a lot of this. Specifically:
* the [`std::remove_reference`], [`std::move`], and [`std::declval`] alternatives
* the [`auto`] and [`declval`] type shenanigans on the [`Result::match`], [`Result::map`] methods to get them to support lambdas. My original version only supported function pointers (see the code in the link above)!
* the other overloads of [`expect`] (I understand that `&` is a reference and `&&` is an rvalue reference (which is a basically thing that can be [`move`]d from?), but I still don't have quite the deep knowledge on it to write all the proper overloads, especially with those being after the function signature?)
* the improved versions of the [`Result`] type's copy and move constructors and its assignment operators (I didn't know about placement new before trying to rewrite the [`Result`] type, and it completely slipped my mind that you had to call the destructor if the [`other`] in the assignment operator was of a different tag!)
Of course, I've read through the code that the AI wrote and it seems logically correct to me, and I've rewritten a lot of it so that it's in my own coding style.
But because I've written it with AI, I don't really trust it.
Could I ask for you guys' help?
I'm only a beginner-intermediate C++ coder,
so is there anything wrong with the code that I don't see?
Thanks in advance!
```
#ifndef RESULT_HPP
#define RESULT_HPP
#include <new>
template<typename P>
void println(P printable);
[[noreturn]] void never() noexcept {
while (true) {
}
}
template <typename M>
[[noreturn]] void panic(M message) noexcept {
println(message);
never();
}
template<typename T> struct remove_reference {
using type = T;
};
template<typename T> struct remove_reference<T &> {
using type = T;
};
template<typename T> struct remove_reference< T && > {
using type = T;
};
template<typename T>
constexpr typename remove_reference<T>::type &&move(T &&arg) noexcept {
return static_cast< typename remove_reference<T>::type && >(arg);
}
template<typename T>
typename remove_reference<T>::type &&declval() noexcept;
template<typename T, typename E>
class Result {
private:
struct OkOverload {
};
struct ErrOverload {
};
public:
static Result ok(const T &ok) {
return Result{
Tag::Ok,
ok,
OkOverload{}
};
}
static Result ok(T &&ok) {
return Result{
Tag::Ok,
move(ok),
OkOverload{}
};
}
static Result err(const E &error) {
return Result{
Tag::Err,
error,
ErrOverload{}
};
}
static Result err(E &&error) {
return Result{
Tag::Err,
move(error),
ErrOverload{}
};
}
template<typename OnOk, typename OnErr>
auto match(OnOk on_ok, OnErr on_err) && -> decltype(on_ok(declval<T>())) {
switch (_tag) {
case Tag::Ok:
return on_ok(move(_value.ok));
case Tag::Err:
return on_err(move(_value.error));
default:
panic("[_tag] was left in an invalid state!");
}
}
template<typename OnOk, typename OnErr>
auto match(OnOk on_ok, OnErr on_err) const & -> decltype(on_ok(declval<const T &>()))
Post #24148
17