hare

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

opaque.ha (1079B)


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