Implementing typestate

Freddy via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 15 15:26:56 PDT 2015


On Tuesday, 15 September 2015 at 21:44:25 UTC, Freddy wrote:
> On Tuesday, 15 September 2015 at 17:45:45 UTC, Freddy wrote:
>>  Rust style memory management in a library
>
> Wait nevermind about that part, it's harder than I thought.

All hope might not be lost, something like this MIGHT work,but 
i'm am sure I am missing some kind of detail.
---
//doesn't actually compile
struct MutBorrow(T, bool usable_ = true)
{
     private T** ptr;
     enum usable = usable_;
     static if (usable)
     {
         @disable this();
         ref T use()
         {
             return **ptr;
         }
     }
     //...
}

struct Unique(T)
{
     private T* ptr;

     alias user = null;

     ~this()
     {
         static assert(user is null);
         free(ptr);
     }

     auto giveMut(alias local)()
     {
         static assert(is(user : null)));
         user = local;
         local.usable = true;
         local.ptr = &ptr;
     }

     auto takeMut(alias local)()
     {
         static assert(local == user); //is there a proper way to 
compare alias?
         user = null;
         local.usable = false;
     }

     static if ( /+user not null+/ )
     {
         ref T use()
         {
             return *ptr;
         }
     }
}
---


More information about the Digitalmars-d mailing list