/* * Copyright 2023 Magnus Madsen * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */////// Trait for types that can be read from, and its functions.///pubmod Readable {use IoError.ErrorKinduse IoError.IoErrorimport java.io.InputStreamimport java.io.IOException////// Trait for types that can be read from.///pubtraitReadable[t] {////// Type of each element that can be read from the resource.///typeElm: Type////// Effect associated with reading from the resource.///typeAef: Eff////// Reads `k` elements from the resource and writes them into `b`.////// Returns `Ok(k)` to signify that `k` bytes were successfully read and written to `b`.////// Guarantees that `0 <= k <= length(b)`.////// Returns `Err(e)` if some underlying I/O error occurs.///pubdefread(buffer: Array[Readable.Elm[t], r], reader: t): Result[IoError, Int32] \ r + Readable.Aef[t] }instanceReadable[InputStream] {typeElm = Int8typeAef = IOpubdefread(b: Array[Int8, r], r: InputStream): Result[IoError, Int32] \ r + IO =try {letresult = r.read(b);letnumRead = if (result== -1) 0elseresult;checked_ecast(Ok(numRead)) } catch {caseex: IOException => Err(IoError(ErrorKind.Other, ex.getMessage())) } }}