diff options
author | John Hodge (sonata) <tpg@mutabah.net> | 2015-01-14 11:17:57 +0800 |
---|---|---|
committer | John Hodge (sonata) <tpg@mutabah.net> | 2015-01-14 11:17:57 +0800 |
commit | b28f21a27b355fef18bb8daf75f447c69ddad8ee (patch) | |
tree | dd115261038500de90e0bedb1100ea906e5ecdb8 /src/common.hpp | |
parent | b3058a63847bd1d2b58765c279b0c29e60eb65ea (diff) | |
download | mrust-b28f21a27b355fef18bb8daf75f447c69ddad8ee.tar.gz |
Local type resolution added, framework for local use statements
Diffstat (limited to 'src/common.hpp')
-rw-r--r-- | src/common.hpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/common.hpp b/src/common.hpp index a7b14fdd..4e3e9be7 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -8,9 +8,64 @@ #include <iostream> #include <vector> +#include <cassert> #define DEBUG(ss) do{ ::std::cerr << __FUNCTION__ << ": " << ss << ::std::endl; } while(0) +namespace rust { + +template<typename T> +class option +{ + bool m_set; + T m_data; +public: + option(T ent): + m_set(true), + m_data( ::std::move(ent) ) + {} + option(): + m_set(false) + {} + + bool is_none() const { return !m_set; } + bool is_some() const { return m_set; } + + const T& unwrap() const { + assert(is_some()); + return m_data; + } +}; +template<typename T> +class option<T&> +{ + T* m_ptr; +public: + option(T& ent): + m_ptr(&ent) + {} + option(): + m_ptr(nullptr) + {} + + bool is_none() const { return m_ptr == nullptr; } + bool is_some() const { return m_ptr != nullptr; } + T& unwrap() const { + assert(is_some()); + return *m_ptr; + } +}; +template<typename T> +option<T> Some(T data) { + return option<T>( ::std::move(data) ); +} +template<typename T> +option<T> None() { + return option<T>( ); +} + +}; + namespace AST { template <typename T> |