From 901c299c40fb30c8de38d7a68710ac60323bd74b Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 13 Dec 2018 01:28:55 +0900 Subject: [PATCH] add unwrap_or to result --- toml/result.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/toml/result.hpp b/toml/result.hpp index e0085f7..90ff1d5 100644 --- a/toml/result.hpp +++ b/toml/result.hpp @@ -394,6 +394,25 @@ struct result return std::move(this->succ.value); } + template + value_type& unwrap_or(U& opt) & + { + if(is_err()) {return opt;} + return this->succ.value; + } + template + value_type const& unwrap_or(U const& opt) const& + { + if(is_err()) {return opt;} + return this->succ.value; + } + template + value_type&& unwrap_or(U&& opt) && + { + if(is_err()) {return std::move(opt);} + return std::move(this->succ.value); + } + error_type& unwrap_err() & { if(is_ok()) {throw std::runtime_error("toml::result: bad unwrap_err");}