@trusted methods

vit vit at vit.vit
Fri Jun 18 12:40:52 UTC 2021


Are asserts enough to make method @trusted or is something like 
throw exception or return error code necessary? How it work in 
phobos?

Example:

```d
struct Slice(T){
	private T[] data;

	this(T[] data){
     	this.data = data;
     }
     inout(T)[] opSlice()inout scope return pure nothrow @safe 
@nogc{
     	return data;
     }
     inout(T)[] opSlice(size_t b, size_t e)inout scope return pure 
nothrow @safe @nogc{
     	return data[b .. e];
     }

     /*
         Remove subset `slice` from `this.data`
     	`slice` is subset of `this.data`.
     */
     void erase1(scope const(T)[] slice)scope pure nothrow 
@trusted @nogc{
         assert(data.ptr <= slice.ptr);
         assert((slice.ptr + slice.length) <= (data.ptr + 
data.length));

         //...
     }

     // ditto
     bool erase2(scope const(T)[] slice)scope pure nothrow 
@trusted @nogc{
         if(data.ptr > slice.ptr)
         	return false;

         if((slice.ptr + slice.length) > (data.ptr + data.length))
             return false;

         //...
         assert(0, "TODO impl");
     }

}


void main()@safe{
     scope slice = Slice!int([1, 2, 3, 4]);
     scope slice2 = Slice!int([1, 2, 3, 4]);


     slice.erase2(slice2[1 .. 2]);	//return false
     slice.erase1(slice2[1 .. 2]);	//assert error in debug

}

```


More information about the Digitalmars-d-learn mailing list