feat: add into method to Option and Result + macro try_unwrap
This commit is contained in:
21
src/ro.h
21
src/ro.h
@ -10,6 +10,7 @@
|
||||
#define None(T) (ro::Option<T>())
|
||||
#define Some(v) (ro::Option(v))
|
||||
#define ERR(fmt, ...) Error(ro::sfmt("%s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__))
|
||||
#define try_unwrap(l,...) try{l=(__VA_ARGS__).unwrap();}catch(ro::Error e){return e;}
|
||||
// #define Err(e) (ro::Result(e))
|
||||
// #define Ok(v) (ro::Result(v))
|
||||
|
||||
@ -181,6 +182,16 @@ namespace ro
|
||||
return fn();
|
||||
}
|
||||
|
||||
inline bool into(T &output) const
|
||||
{
|
||||
if(is_some())
|
||||
{
|
||||
output = m_some;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
inline Option<U> map(const std::function<U(const T &)> &fn)
|
||||
{
|
||||
@ -404,6 +415,16 @@ namespace ro
|
||||
return alt;
|
||||
}
|
||||
|
||||
inline bool into(T &output) const
|
||||
{
|
||||
if(is_ok())
|
||||
{
|
||||
output = m_result;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline T unwrap_or_else(const std::function<T(const E &)> &fn)
|
||||
{
|
||||
if (is_ok())
|
||||
|
Reference in New Issue
Block a user