Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.99 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  rustic
  4. //
  5. //  Created by Antony Searle on 21/7/18.
  6. //  Copyright © 2018 Antony Searle. All rights reserved.
  7. //
  8.  
  9. #include <utility>
  10. #include <iostream>
  11. #include <cassert>
  12. #include <mutex>
  13.  
  14. namespace rust {
  15.    
  16.     void panic() {
  17.         std::terminate();
  18.     }
  19.    
  20.     void panic(const char* msg) {
  21.         std::cerr << msg << std::endl;
  22.         std::terminate();
  23.     }
  24.    
  25.     template<typename T>
  26.     struct Cell {
  27.        
  28.         Cell(T&& value)
  29.         : _value(std::move(value)) {
  30.         }
  31.        
  32.         T get() const& {
  33.             return std::as_const(this->_value);
  34.         }
  35.        
  36.         void set(T&& value) const& {
  37.             _value = std::move(value);
  38.         }
  39.        
  40.         T& get_mut() & {
  41.             return this->_value;
  42.         }
  43.        
  44.         void swap(Cell<T> const& other) const& {
  45.             std::swap(_value, other._value);
  46.         }
  47.        
  48.         T replace(T&& value) const& {
  49.             return std::exchange(_value, std::move(value));
  50.         }
  51.        
  52.         T into_inner() && {
  53.             return std::move(_value);
  54.         }
  55.        
  56.         T take() const& {
  57.             return std::exchange(_value, T());
  58.         }
  59.        
  60.         static Cell from(T&& value) {
  61.             return Cell(std::move(value));
  62.         }
  63.        
  64.         Cell clone() const& {
  65.             return Cell(get());
  66.         }
  67.        
  68.         Cell() : _value() {}
  69.        
  70.     private:
  71.        
  72.         mutable T _value;
  73.        
  74.     };
  75.    
  76.     template<typename T>
  77.     std::ostream& operator<<(std::ostream& a, Cell<T> const& b) {
  78.         return a << "Cell(" << b.get() << std::endl;
  79.     }
  80.    
  81.     template<typename T>
  82.     struct Option {
  83.     private:
  84.         std::aligned_storage_t<sizeof(T), alignof(T)> _value;
  85.         bool _is_some;
  86.         bool is_some() const& { return _is_some; }
  87.         bool is_none() const& { return !_is_some; }
  88.         Option<T const&> as_ref() const&;
  89.         Option<T &> as_mut() &;
  90.         T expect(const char* msg) && {
  91.             if (!_is_some)
  92.                 panic();
  93.             return reinterpret_cast<T&&>(_value);
  94.         }
  95.         T unwrap(const char* msg) && {
  96.             if (!_is_some)
  97.                 panic();
  98.             return reinterpret_cast<T&&>(_value);
  99.         }
  100.         T unwrap_or(T&& def) && {
  101.             if (!_is_some)
  102.                 return std::move(def);
  103.             return reinterpret_cast<T&&>(_value);
  104.         }
  105.         template<typename F>
  106.         T unwrap_or_else(F&& f) && {
  107.             if (_is_some)
  108.                 return std::forward<F>(f)();
  109.             return reinterpret_cast<T&&>(_value);
  110.         }
  111.        
  112.         template<typename U, typename F>
  113.         Option<U> map(F&& f) &&;
  114.        
  115.         template<typename U, typename F>
  116.         U map(U&& def, F&& f) &&;
  117.        
  118.        
  119.     };
  120.    
  121.     template<typename T, typename E>
  122.     struct Result {
  123.     private:
  124.         union {
  125.             T _ok;
  126.             E _err;
  127.         };
  128.         bool _is_err;
  129.     public:
  130.         bool is_ok() const& { return !_is_err; }
  131.         bool is_err() const& { return _is_err; }
  132.     };
  133.    
  134.    
  135.     template<typename T>
  136.     struct Ref;
  137.    
  138.     template<typename T>
  139.     struct RefMut;
  140.    
  141.     struct BorrowError {};
  142.     struct BorrowMutError {};
  143.    
  144.  
  145.     template<typename T>
  146.     struct RefCell {
  147.        
  148.         RefCell()
  149.         : _value()
  150.         , _borrowers(0) {
  151.         }
  152.        
  153.         explicit RefCell(T&& value)
  154.         : _value(std::move(value))
  155.         , _borrowers(0) {
  156.         }
  157.        
  158.         ~RefCell() {
  159.             assert(!_borrowers);
  160.         }
  161.        
  162.         T into_inner() && {
  163.             assert(!_borrowers);
  164.             return std::move(this->_value);
  165.         }
  166.    
  167.         T replace(T&& value) const& {
  168.             if (_borrowers)
  169.                 panic();
  170.             return std::exchange(this->_value, std::move(value));
  171.         }
  172.        
  173.         void swap(RefCell<T> const& other) const& {
  174.             if (_borrowers || other._borrowers)
  175.                 panic();
  176.             std::swap(this->_value, other->_value);
  177.         }
  178.        
  179.         Ref<T> borrow() const&;
  180.        
  181.         Result<Ref<T>, BorrowError> try_borrow() const&;
  182.        
  183.         RefMut<T> borrow_mut() const&;
  184.        
  185.         Result<RefMut<T>, BorrowMutError> try_borrow_mut() const&;
  186.        
  187.         T* as_ptr() const& { return &_value; }
  188.        
  189.         T& get_mut() & { return _value; }
  190.        
  191.     private:
  192.        
  193.         mutable T _value;
  194.         mutable std::ptrdiff_t _borrowers;
  195.        
  196.         friend class Ref<T>;
  197.         friend class RefMut<T>;
  198.     };
  199.    
  200.     template<typename T>
  201.     std::ostream& operator<<(std::ostream& a, RefCell<T> const& b) {
  202.         return a << "RefCell { value: " << *b.borrow() << "}";
  203.     }
  204.    
  205.     template<typename T>
  206.     struct Ref {
  207.         static Ref clone(Ref const& orig) {
  208.             return Ref(orig);
  209.         }
  210.         Ref() = delete;
  211.         Ref(Ref const& other) : _cell(other._cell) {
  212.             assert(_cell);
  213.             ++_cell->_borrowers;
  214.         }
  215.         Ref(Ref&& other) : _cell(std::exchange(other._cell, nullptr)) {}
  216.         ~Ref() {
  217.             if (_cell) {
  218.                 assert(_cell->_borrowers > 0);
  219.                 --_cell->_borrowers;
  220.             }
  221.         }
  222.        
  223.         T const& operator*() const& {
  224.             assert(_cell);
  225.             return _cell->_value;
  226.         }
  227.        
  228.         operator T const&() const& {
  229.             assert(_cell);
  230.             return _cell->_value;
  231.         }
  232.  
  233.        
  234.         T const* operator->() const& {
  235.             assert(_cell);
  236.             return &_cell->_value;
  237.         }
  238.        
  239.     private:
  240.        
  241.         friend class RefCell<T>;
  242.         explicit Ref(RefCell<T> const* cell)
  243.         : _cell(cell) {
  244.             assert(cell);
  245.         }
  246.         RefCell<T> const* _cell;
  247.        
  248.     };
  249.    
  250.     template<typename T>
  251.     std::ostream& operator<<(std::ostream& a, Ref<T> const& b) {
  252.         return a << *b;
  253.     }
  254.  
  255.    
  256.     template<typename T>
  257.     struct RefMut {
  258.         RefMut() = delete;
  259.         RefMut(RefMut const& other) = delete;
  260.         RefMut(RefMut && other)
  261.         : _cell(std::exchange(other._cell, nullptr)) {
  262.             assert(_cell);
  263.         }
  264.         ~RefMut() {
  265.             if (_cell) {
  266.                 assert(_cell->_borrowers == -1);
  267.                 _cell->_borrowers = 0;
  268.             }
  269.         }
  270.        
  271.         T const& operator*() const& {
  272.             assert(_cell);
  273.             return _cell->_value;
  274.         }
  275.        
  276.         T const* operator->() const& {
  277.             assert(_cell);
  278.             return &_cell->_value;
  279.         }
  280.        
  281.         operator T const&() const& {
  282.             assert(_cell);
  283.             return _cell->_value;
  284.         }
  285.  
  286.         operator T&() & {
  287.             assert(_cell);
  288.             return _cell->_value;
  289.         }
  290.  
  291.         T& operator*() & {
  292.             assert(_cell);
  293.             return _cell->_value;
  294.         }
  295.        
  296.         T* operator->() & {
  297.             assert(_cell);
  298.             return _cell->_value;
  299.         }
  300.        
  301.     private:
  302.         explicit RefMut(RefCell<T> const* cell)
  303.         : _cell(cell) {
  304.             assert(cell);
  305.         }
  306.         RefCell<T> const* _cell;
  307.         friend class RefCell<T>;
  308.     };
  309.    
  310.     template<typename T>
  311.     std::ostream& operator<<(std::ostream& a, RefMut<T> const& b) {
  312.         return a << *b;
  313.     }
  314.  
  315.    
  316.     template<typename T>
  317.     Ref<T> RefCell<T>::borrow() const& {
  318.         if (_borrowers == -1) {
  319.             panic("Can't borrow RefCell because it is already mutably borrowed");
  320.         }
  321.         ++_borrowers;
  322.         return Ref<T>(this);
  323.     }
  324.    
  325.     template<typename T>
  326.     RefMut<T> RefCell<T>::borrow_mut() const& {
  327.         if (_borrowers != 0) {
  328.             panic("Can't mutably borrow RefCell because it is already borrowed");
  329.         }
  330.         --_borrowers;
  331.         return RefMut<T>(this);
  332.     }
  333.    
  334.     template<typename T>
  335.     void drop(T& value) {
  336.         T _value(std::move(value));
  337.     }
  338.    
  339.    
  340.     template<typename T>
  341.     struct Box {
  342.        
  343.         Box() : _ptr(new T()) {}
  344.        
  345.         Box(Box const&) = delete;
  346.        
  347.         Box(Box&& other)
  348.         : _ptr(std::exchange(other._ptr, nullptr)) {
  349.         }
  350.        
  351.         ~Box() {
  352.             delete _ptr;
  353.         }
  354.        
  355.         Box& operator=(Box const&) = delete;
  356.        
  357.         Box& operator=(Box&& other) {
  358.             _ptr = std::exchange(other._ptr, nullptr);
  359.             return *this;
  360.         }
  361.        
  362.         explicit Box(T&& x)
  363.         : _ptr(new T(std::move(x))) {
  364.         }
  365.        
  366.         static Box from_raw(T* raw) {
  367.             assert(raw);
  368.             return Box(raw);
  369.         }
  370.        
  371.         static T* into_raw(Box<T>&& b) {
  372.             assert(b._ptr);
  373.             return std::exchange(b._ptr, nullptr);
  374.         }
  375.        
  376.         static T& leak(Box<T>&& b) {
  377.             assert(b._ptr);
  378.             return *std::exchange(b._ptr, nullptr);
  379.         }
  380.        
  381.         const T& operator*() const & {
  382.             assert(_ptr);
  383.             return *_ptr;
  384.         }
  385.        
  386.         operator const T& () const& {
  387.             assert(_ptr);
  388.             return *_ptr;
  389.         }
  390.  
  391.         T& operator*() & {
  392.             assert(_ptr);
  393.             return *_ptr;
  394.         }
  395.  
  396.         operator T& () & {
  397.             assert(_ptr);
  398.             return *_ptr;
  399.         }
  400.  
  401.         T const* operator->() const& {
  402.             assert(_ptr);
  403.             return _ptr;
  404.         }
  405.  
  406.         T* operator->() & {
  407.             assert(_ptr);
  408.             return _ptr;
  409.         }
  410.        
  411.     private:
  412.        
  413.         explicit Box(T* raw)
  414.         : _ptr(raw) {
  415.         }
  416.        
  417.         T* _ptr;
  418.        
  419.     }; // Box
  420.    
  421.     template<typename T>
  422.     std::ostream& operator<<(std::ostream& a, Box<T> const& b) {
  423.         return a << *b;
  424.     }
  425.  
  426.     template<typename T>
  427.     struct Rc {
  428.        
  429.     private:
  430.        
  431.         struct holder {
  432.             T _value;
  433.             std::ptrdiff_t _count;
  434.            
  435.             holder()
  436.             : _value()
  437.             , _count(0) {
  438.             }
  439.            
  440.             holder(T&& value)
  441.             : _value(std::move(value))
  442.             , _count(0) {}
  443.            
  444.         };
  445.        
  446.         holder _ptr;
  447.        
  448.         explicit Rc(holder* p) : _ptr(p) {}
  449.        
  450.     public:
  451.         Rc() : _ptr(new holder()) {}
  452.         Rc(Rc const&) = delete;
  453.         Rc(Rc&& other) : _ptr(other._ptr) { assert(_ptr); ++_ptr->_count; }
  454.         ~Rc() { delete _ptr; }
  455.         static Result<T, Rc<T>> try_unwrap(Rc&&);
  456.         static T const* into_raw(Rc&& r) {
  457.             return &r._ptr._value;
  458.         }
  459.         static Rc from_raw(T const* p) {
  460.             return Rc(reinterpret_cast<holder const*>(p));
  461.         }
  462.        
  463.         size_t strong_count() const& { assert(_ptr); return _ptr->_count + 1; }
  464.         static Option<T&> get_mut(Rc<T>&);
  465.        
  466.         static T& make_mut(Rc<T>& r) {
  467.             assert(r._ptr);
  468.             if (r._ptr->_count) {
  469.                 --r._ptr->_count;
  470.                 r._ptr = new holder(T(r._ptr._value));
  471.             }
  472.             return r._ptr->_value;
  473.         }
  474.        
  475.         T const& operator*() const& { assert(_ptr); return _ptr->_value; }
  476.         operator T const&() const& { assert(_ptr); return  _ptr->_value; }
  477.         T const* operator->() const& { assert(_ptr); return &_ptr->_value; }
  478.        
  479.     };
  480.    
  481.     using usize = size_t;
  482.    
  483.     template<typename T>
  484.     struct Slice {
  485.     private:
  486.         T* _begin;
  487.         usize _len;
  488.     public:
  489.         usize len() const& { return _len; }
  490.         bool is_empty() const& { return _len != 0; }
  491.     };
  492.    
  493.     template<typename T>
  494.     struct Vec {
  495.     private:
  496.         std::vector<T> _vector;
  497.         Vec(const Vec&) = default;
  498.         Vec& operator=(const Vec&) = default;
  499.     public:
  500.         Vec() = default;
  501.         Vec(Vec&& other) = default;
  502.         ~Vec() = default;
  503.         Vec& operator=(Vec&& other) = default;
  504.        
  505.         static Vec with_capacity(usize capacity) { Vec v; v._vector.reserve(capacity); return v; }
  506.        
  507.         usize capacity() const& { return _vector.capacity(); }
  508.         void reserve(usize additional) & { _vector.reserve(_vector.size() + additional); }
  509.         void shrink_to_fit() & { _vector.shrink_to_fit(); }
  510.         void truncate(usize len) & { if (len < _vector.size()) _vector.resize(len); }
  511.         T swap_remove(usize index) & {
  512.             if (index >= _vector.size())
  513.                 panic("Out of bounds");
  514.             T result(std::move(_vector[index]));
  515.             _vector[index] = std::move(_vector.back());
  516.             _vector.pop_back();
  517.             return result;
  518.         }
  519.         void insert(usize index, T&& element) & {
  520.             _vector.insert(_vector.cbegin() + index, std::move(element));
  521.         }
  522.         T remove(usize index) & {
  523.             auto p = _vector.begin() + index;
  524.             T v(std::move(*p));
  525.             _vector.erase(p);
  526.             return v;
  527.         }
  528.         void push(T&& value) & {
  529.             _vector.push_back(std::move(value));
  530.         }
  531.         Option<T> pop() &;
  532.         void append(Vec<T>& other) & {
  533.             reserve(other._vector.size());
  534.             std::move(other.begin(), other.end(), std::back_inserter(_vector));
  535.             other._vector.clear();
  536.         }
  537.         void clear() & { _vector.clear(); }
  538.         usize len() const& { return _vector.size(); }
  539.         bool is_empty() const& { return _vector.empty(); }
  540.         Vec split_off(usize at) & {
  541.             if (at > _vector.size())
  542.                 panic();
  543.             Vec v = Vec::with_capacity(_vector.size() - at);
  544.             std::move(_vector.begin() + at, _vector.end(), std::back_inserter(v._vector));
  545.             _vector.resize(at);
  546.             return v;
  547.         }
  548.         void dedup() & {
  549.             _vector.erase(std::unique(_vector.begin(), _vector.end()), _vector.end());
  550.         }
  551.         Option<T const&> first() const&;
  552.         Option<T&> first_mut() &;
  553.         Option<T const&> last() const&;
  554.         Option<T&> last_mut() &;
  555.         void reverse() & { std::reverse(_vector.begin(), _vector.end()); }
  556.         Vec clone() const& {
  557.             return Vec(*this);
  558.         }
  559.  
  560.         T& operator[](usize index) & {
  561.             if (index >= _vector.size())
  562.                 panic();
  563.             return _vector[index];
  564.         }
  565.        
  566.         T const& operator[](usize index) const& {
  567.             if (index >= _vector.size())
  568.                 panic();
  569.             return _vector[index];
  570.         }
  571.  
  572.     }; // struct Vec<T>
  573.    
  574.     template<typename T>
  575.     struct Mutex;
  576.    
  577.     template<typename T>
  578.     struct MutexGuard {
  579.        
  580.     private:
  581.        
  582.         Mutex<T> const* _ptr;
  583.        
  584.         explicit MutexGuard(Mutex<T> const& p)
  585.         : _ptr(&p) {
  586.             assert(_ptr);
  587.             _ptr->_mutex.lock();
  588.         }
  589.        
  590.         friend class Mutex<T>;
  591.        
  592.     public:
  593.        
  594.         MutexGuard(MutexGuard const&) = delete;
  595.        
  596.         MutexGuard(MutexGuard&& other)
  597.         : _ptr(std::exchange(other._ptr, nullptr)) {
  598.         }
  599.        
  600.         ~MutexGuard() {
  601.             if (_ptr)
  602.                 _ptr->_mutex.unlock();
  603.         }
  604.        
  605.         MutexGuard& operator=(MutexGuard const& other) {
  606.             assert(_ptr);
  607.             assert(other._ptr);
  608.             _ptr->_value = other._ptr->_value;
  609.             return *this;
  610.         }
  611.        
  612.         operator T const&() const& {
  613.             assert(_ptr);
  614.             return _ptr->_value;
  615.         }
  616.        
  617.         T const& operator*() const& {
  618.             assert(_ptr);
  619.             return _ptr->_value;
  620.         }
  621.        
  622.         T const* operator->() const& {
  623.             assert(_ptr);
  624.             return &_ptr->_value;
  625.         }
  626.  
  627.         operator T&() & {
  628.             assert(_ptr);
  629.             return _ptr->_value;
  630.         }
  631.        
  632.         T& operator*() & {
  633.             assert(_ptr);
  634.             return _ptr->_value;
  635.         }
  636.        
  637.         T* operator->() & {
  638.             assert(_ptr);
  639.             return &_ptr->_value;
  640.         }
  641.        
  642.     };
  643.    
  644.     template<typename T>
  645.     struct Mutex {
  646.        
  647.     private:
  648.        
  649.         friend class MutexGuard<T>;
  650.         mutable T _value;
  651.         mutable std::mutex _mutex;
  652.        
  653.     public:
  654.        
  655.         Mutex() = default;
  656.         Mutex(Mutex const&) = delete;
  657.         Mutex& operator=(Mutex const&) = delete;
  658.        
  659.         explicit Mutex(T&& value) : _value(std::move(value)) {}
  660.        
  661.         MutexGuard<T> lock() const& {
  662.             return MutexGuard<T>(this);
  663.         }
  664.        
  665.         T into_inner() && {
  666.             return std::move(_value);
  667.         }
  668.        
  669.         // No locking occurs because we already have mutable therefore exclusive
  670.         // access
  671.         T& get_mut() & {
  672.             return _value;
  673.         }
  674.        
  675.     }; // Mutex<T>
  676.    
  677.    
  678.    
  679.    
  680.    
  681.    
  682.    
  683.    
  684.     void main() {
  685.         {
  686.             auto a = RefCell(7);
  687.             {
  688.                 auto b = a.borrow();
  689.                 std::cout << b << std::endl;
  690.                 std::cout << a << std::endl;
  691.                 // auto c = a.borrow_mut();
  692.                 auto d = a.borrow();
  693.                 std::cout << d << std::endl;
  694.                 // auto e = RefCell<int>(d);
  695.                 std::cout << (d + 1) << std::endl;
  696.                
  697.             }
  698.             {
  699.                 auto c = a.borrow_mut();
  700.                 c += 2;
  701.                 std::cout << c << std::endl;
  702.                 // std::cout << a << std::endl;
  703.             }
  704.         }
  705.         {
  706.             auto a = Box(99);
  707.             std::cout << a << std::endl;
  708.             drop(a);
  709.             // std::cout << a << std::endl;
  710.         }
  711.     }
  712.    
  713. }
  714.  
  715. int main(int argc, const char * argv[]) {
  716.     rust::main();
  717.     return 0;
  718. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement