#! @file result.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the properties and methods of `result`, and `ok` and `err`, which #! make one holding a value or an error. #! #! `result`, which holds either the value an operation gave or the error that #! stopped it, is among the types the syntax makes, which the compiler declares #! itself. It must be handled where it is made, since letting it go would lose #! the error it may hold: by `match`, by `is`, or by `else`, as #! `val f := open(path) else (e) { return e; };`. #! #! The methods reading what it holds, as `unwrap`, move it out of what can be #! written, as `move` does, and copy it out of what is only read, which a value #! that closes refuses. Those making another `result` of it, as `map`, move it #! in, as an iterator's adapters do: `as_ref` lends them what one that is only #! read holds. Each makes what it gives by naming the field, so a `result` whose #! value and error are of one type is told apart. module core; #! Makes a result holding a value. #! @param value The value to hold. #! @returns The result, holding the value. pub def result::ok(value: T): result { return result:: { value = move(&mut value) }; } #! Makes a result holding an error. #! @param error The error to hold. #! @returns The result, holding the error. pub def result::err(error: E): result { return result:: { error = move(&mut error) }; } #! Whether it holds the value, where the work succeeded. #! @param this The result. #! @returns Whether it does. pub val result::is_ok: bool { get => this is value; } #! Whether it holds the error, where the work failed. #! @param this The result. #! @returns Whether it does. pub val result::is_err: bool { get => this is error; } #! Whether it holds a value a test passes. #! @tparam F The test. #! @param this The result. #! @param test The test, given a reference to the value. #! @returns Whether it holds one that passes. pub def result::is_ok_and(&this, test: F): bool where F: invokable { return this is value(&held) && test(held); } #! Whether it holds an error a test passes. #! @tparam F The test. #! @param this The result. #! @param test The test, given a reference to the error. #! @returns Whether it holds one that passes. pub def result::is_err_and(&this, test: F): bool where F: invokable { return this is error(&held) && test(held); } #! Gives the value it holds, moved out as `move` moves it. It stops the program #! where it holds an error. #! @param this The result. #! @returns The value. pub def result::unwrap(&mut this): T { var held := move(this); return move(got) if held is value(&mut got); fault("A `result` holding an error was unwrapped."); } #! Gives a copy of the value it holds, which a value that closes refuses. It #! stops the program where it holds an error. #! @param this The result. #! @returns The value. pub def result::unwrap(&this): T { if const T is closeable { copied::(); } else { return got if this is value(got); } fault("A `result` holding an error was unwrapped."); } #! Gives the value it holds, moved out as `unwrap` moves it. It stops the #! program where it holds an error, saying why. #! @param this The result. #! @param why What went wrong, where it holds an error. #! @returns The value. pub def result::expect(&mut this, why: strview): T { var held := move(this); return move(got) if held is value(&mut got); fault(why); } #! Gives a copy of the value it holds, which a value that closes refuses. It #! stops the program where it holds an error, saying why. #! @param this The result. #! @param why What went wrong, where it holds an error. #! @returns The value. pub def result::expect(&this, why: strview): T { if const T is closeable { copied::(); } else { return got if this is value(got); } fault(why); } #! Gives the error it holds, moved out as `move` moves it. It stops the program #! where it holds a value. #! @param this The result. #! @returns The error. pub def result::unwrap_err(&mut this): E { var held := move(this); return move(got) if held is error(&mut got); fault("A `result` holding a value was unwrapped for its error."); } #! Gives a copy of the error it holds, which an error that closes refuses. It #! stops the program where it holds a value. #! @param this The result. #! @returns The error. pub def result::unwrap_err(&this): E { if const E is closeable { copied::(); } else { return got if this is error(got); } fault("A `result` holding a value was unwrapped for its error."); } #! Gives the error it holds, moved out as `unwrap_err` moves it. It stops the #! program where it holds a value, saying why. #! @param this The result. #! @param why What went wrong, where it holds a value. #! @returns The error. pub def result::expect_err(&mut this, why: strview): E { var held := move(this); return move(got) if held is error(&mut got); fault(why); } #! Gives a copy of the error it holds, which an error that closes refuses. It #! stops the program where it holds a value, saying why. #! @param this The result. #! @param why What went wrong, where it holds a value. #! @returns The error. pub def result::expect_err(&this, why: strview): E { if const E is closeable { copied::(); } else { return got if this is error(got); } fault(why); } #! Gives the value it holds, moved out as `unwrap` moves it, or a fallback #! where it holds an error. #! @param this The result. #! @param fallback The value given where it holds an error. #! @returns The value, or the fallback. pub def result::unwrap_or(&mut this, fallback: T): T { var held := move(this); return move(got) if held is value(&mut got); return move(&mut fallback); } #! Gives a copy of the value it holds, which a value that closes refuses, or a #! fallback where it holds an error. #! @param this The result. #! @param fallback The value given where it holds an error. #! @returns The value, or the fallback. pub def result::unwrap_or(&this, fallback: T): T { if const T is closeable { copied::(); } else { return got if this is value(got); } return move(&mut fallback); } #! Gives the value it holds, moved out as `unwrap` moves it, or what a function #! makes of the error, which it is called on only then. #! @tparam F The function. #! @param this The result. #! @param make The function, given the error, moved out. #! @returns The value, or what the function made. pub def result::unwrap_or_else(&mut this, make: F): T where F: invokable { var held := move(this); return match held { value(&mut got) => move(got), error(&mut bad) => make(move(bad)) }; } #! Lends what it holds: a reference to the value or to the error. #! @param this The result. #! @returns The result of the references. pub def result::as_ref(&this): result<&T, &E> { return match this { value(&got) => result::<&T, &E> { value = got }, error(&bad) => result::<&T, &E> { error = bad } }; } #! Lends what it holds to be written: a reference to the value or to the #! error. #! @param this The result. #! @returns The result of the references. pub def result::as_mut(&mut this): result<&mut T, &mut E> { return match this { value(&mut got) => result::<&mut T, &mut E> { value = got }, error(&mut bad) => result::<&mut T, &mut E> { error = bad } }; } #! Makes a value of the value it holds, which a function is given, moved in, #! keeping the error it holds otherwise. #! @tparam U What the function makes. #! @tparam F The function. #! @param this The result, moved in as `unwrap` moves it. #! @param f The function, called on the value where it holds one. #! @returns The result of what the function made, or of the error. pub def result::map(&mut this, f: F): result where F: invokable { var held := move(this); return match held { value(&mut got) => result:: { value = f(move(got)) }, error(&mut bad) => result:: { error = move(bad) } }; } #! Makes another error of the error it holds, which a function is given, moved #! in, keeping the value it holds otherwise. #! @tparam D What the function makes. #! @tparam F The function. #! @param this The result, moved in as `unwrap` moves it. #! @param f The function, called on the error where it holds one. #! @returns The result of the value, or of what the function made. pub def result::map_err(&mut this, f: F): result where F: invokable { var held := move(this); return match held { value(&mut got) => result:: { value = move(got) }, error(&mut bad) => result:: { error = f(move(bad)) } }; } #! Gives what a function makes of the value it holds, which may fail too: the #! next step of work that may fail, keeping the error it holds otherwise. #! @tparam U What the function may make. #! @tparam F The function. #! @param this The result, moved in as `unwrap` moves it. #! @param f The function, called on the value where it holds one. #! @returns What the function gave, or the result of the error. pub def result::and_then(&mut this, f: F): result where F: invokable> { var held := move(this); return match held { value(&mut got) => f(move(got)), error(&mut bad) => result:: { error = move(bad) } }; } #! Gives what a function makes of the error it holds, which may recover from #! it, keeping the value it holds otherwise. #! @tparam D The error of what the function makes. #! @tparam F The function. #! @param this The result, moved in as `unwrap` moves it. #! @param f The function, called on the error where it holds one. #! @returns The result of the value, or what the function gave. pub def result::or_else(&mut this, f: F): result where F: invokable> { var held := move(this); return match held { value(&mut got) => result:: { value = move(got) }, error(&mut bad) => f(move(bad)) }; } #! Gives the value it holds, as a `maybe`, losing the error. #! @param this The result, moved in as `unwrap` moves it. #! @returns Maybe the value; otherwise, `nothing` where it holds an error. pub def result::to_maybe(&mut this): maybe { var held := move(this); return maybe:: { some = move(got) } if held is value(&mut got); return nothing {}; } #! Gives the error it holds, as a `maybe`, losing the value. #! @param this The result, moved in as `unwrap` moves it. #! @returns Maybe the error; otherwise, `nothing` where it holds a value. pub def result::err_to_maybe(&mut this): maybe { var held := move(this); return maybe:: { some = move(got) } if held is error(&mut got); return nothing {}; } #! Whether it holds what another does: equal values, or equal errors, as `==` #! compares them. #! @param this The result. #! @param other The other. #! @returns Whether they are equal. #! @remarks A `result` whose value or error is of a type that is not #! `equatable` is an error where it is compared. pub def result::equals(&this, other: &result): bool { return match this { value(&mine) => other is value(&theirs) && equals::(mine, theirs), error(&mine) => other is error(&theirs) && equals::(mine, theirs) }; } #! Hashes what it holds, so a value hashes apart from an error. #! @param this The result. #! @returns The hash. #! @remarks A `result` whose value or error is of a type that is not `hashable` #! is an error where it is hashed. pub def result::hash(&this): u64 { return match this { value(&got) => mix(1, hash::(got)), error(&bad) => mix(2, hash::(bad)) }; } #! Builds a copy of a result, holding a copy of its value or its error, as #! `copy` makes it. #! @param other The result to copy. #! @returns The copy. pub def result::copy_of(other: &result): result { return match other { value(&got) => result:: { value = copy::(got) }, error(&bad) => result:: { error = copy::(bad) } }; }