hare

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

types.ha (2894B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 
      6 // Returned by [[readall]] if the I/O handle returned [[EOF]] prior to
      7 // completely reading an item. Stores the amount that was succesfully read.
      8 export type underread = !size;
      9 
     10 // Any error which may be returned from an I/O function.
     11 export type error = !(...errors::error | underread);
     12 
     13 // Indicates an end-of-file condition.
     14 export type EOF = done;
     15 
     16 // Converts an I/O [[error]] into a user-friendly string.
     17 export fn strerror(err: error) str = {
     18 	match (err) {
     19 	case underread =>
     20 		return "Insufficient data to read entire item";
     21 	case let err: errors::error =>
     22 		return errors::strerror(err);
     23 	};
     24 };
     25 
     26 // Used to indicate if a stream should be used for reading, or writing, or both.
     27 export type mode = enum u8 {
     28 	NONE	= 0,
     29 	READ	= 1 << 0,
     30 	WRITE	= 1 << 1,
     31 	RDWR	= READ | WRITE,
     32 };
     33 
     34 // From "whence" a seek operation should occur.
     35 export type whence = enum {
     36 	SET = 0, // Relative to beginning (i.e. set absolute position).
     37 	CUR = 1, // Relative to current position.
     38 	END = 2, // Relative to end of handle.
     39 };
     40 
     41 // The interface for a stream which can be read from. Reads up to len(buf)
     42 // bytes from the reader into the given buffer, returning the number of bytes
     43 // read or an error.
     44 export type reader = fn(s: *stream, buf: []u8) (size | EOF | error);
     45 
     46 // The interface for a stream which can be written to. Writes up to len(buf)
     47 // bytes to the writer from the given buffer, returning the number of bytes
     48 // written or an error.
     49 export type writer = fn(s: *stream, buf: const []u8) (size | error);
     50 
     51 // The interface for a stream which can be closed. This function should close
     52 // and free any underlying resources, and cannot be used again.
     53 export type closer = fn(s: *stream) (void | error);
     54 
     55 // The interface for a stream which has first-class support for copying data
     56 // from another stream. Often this only works if the second stream is of the
     57 // same underlying stream type. This is optional, [[copy]] still works even with
     58 // a stream which does not implement this (it falls back to calling read and
     59 // write in a loop).
     60 //
     61 // Returns the number of bytes copied, or an error if one occured. Do not close
     62 // either stream. If the operation is unsupported for this particular pair of
     63 // streams, return [[errors::unsupported]] to have [[copy]] proceed with its
     64 // fallback implementation.
     65 export type copier = fn(to: *stream, from: *stream) (size | error);
     66 
     67 // The interface for a stream which can be seeked. Sets the offset for the next
     68 // read or write to offset, interpreted according to whence:
     69 // whence::SET means relative to the start of the file,
     70 // whence::CUR means relative to the current offset, and
     71 // whence::END means relative to the end.
     72 //
     73 // Returns the new offset relative to the start or an error.
     74 export type seeker = fn(s: *stream, off: off, w: whence) (off | error);