hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

opaque.ha (1098B)


      1 // License: MPL-2.0
      2 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      3 // (c) 2021 Eyal Sawady <ecs@d2evs.net>
      4 
      5 // An "opaque" error is used as a portable error type for an underlying error
      6 // which is implementation-specific. It provides a function which can be used to
      7 // produce a string describing the error, and a small storage area for arbitrary
      8 // implementation-specific storage.
      9 //
     10 // The following example shows the usage of this type for custom errors:
     11 //
     12 // 	fn wraperror(err: myerror) error::opaque = {
     13 // 		static assert(size(myerror) <= size(errors::opaque_data));
     14 // 		let wrapped = errors::opaque { strerror = &opaque_strerror, ... };
     15 // 		let myptr = &wrapped.data: *myerror;
     16 // 		*myptr = err;
     17 // 		return wrapped;
     18 // 	};
     19 //
     20 // 	fn opaque_strerror(err: *errors::opaque_data) const str = {
     21 // 		let ptr = &err: *myerr;
     22 // 		return strerror(*ptr);
     23 // 	};
     24 export type opaque = !struct {
     25 	strerror: *fn(op: *opaque_data) const str,
     26 	data: opaque_data,
     27 };
     28 
     29 // Up to 24 bytes of arbitrary data that the opaque error type may use for
     30 // domain-specific storage.
     31 export type opaque_data = [24]u8;