window

source of std::result

1// The outcome of something that may fail: a value (`Ok`) or the error2// that took its place (`Err`).3//4// A function that can fail returns a Result and its caller matches on it;5// there is no operator propagating an error, so a caller passing one on6// writes the match. Like `Option`, it is an ordinary generic enum, and7// `==` compares two results by their variant and payload. It is written8// out (std::format) when both its payloads are, and disposable (section9// 5.2) when either is.10//11// ```alloy12// import std::text;13// import std::string;14//15// const port = (text: &[u8]) -> Result<u16, String> {16//     if (text.parse_integer() is ::Some |n|) {17//         if (n >= 0 && n < 65536) {18//             return ::Ok(n to u16);19//         }20//     }21//     return ::Err(String::from(&"not a port"));22// };23// match (port(&"8080")) {24//     ::Ok |number| { expect_equal(&number, &8080); }25//     ::Err { expect(false); }26// }27// expect(port(&"http").is_err());28// ```2930import std::option;31import std::string;32import std::format;33import std::dispose;3435exp type Result<T, E> : Format if (36    T: Format37    && E: Format38) + Disposable if (T: Disposable || E: Disposable) = enum {39    Ok: T,40    Err: E,41};4243// Disposes the value or the error it holds, when that one is disposable.44exp fn dispose<T, E>(self result: &var Result<T, E>) {45    if (result is ::Ok |&var value|) {46        if (T: Disposable) {47            value.dispose();48        }49    } else if (result is ::Err |&var error|) {50        if (E: Disposable) {51            error.dispose();52        }53    }54}5556// Writes `Ok(value)` or `Err(error)` (std::format).57//58// ```alloy59// const done: Result<u32, bool> = ::Ok(3);60// const failed: Result<u32, bool> = ::Err(true);61// expect_equal(&f"{done} {failed}".view(), &"Ok(3) Err(true)");62// ```63exp fn format<T: Format, E: Format>(self result: &Result<T, E>, out: &var String) {64    match (result) {65        ::Ok |&value| {66            out.append(&"Ok(");67            value.format(&var out);68        }69        ::Err |&error| {70            out.append(&"Err(");71            error.format(&var out);72        }73    }74    out.push(')');75}7677// True when the result holds a value.78//79// ```alloy80// const done: Result<u32, u8> = ::Ok(3);81// expect(done.is_ok());82// ```83exp fn is_ok<T, E>(self result: &Result<T, E>) -> bool => result is ::Ok;8485// True when the result holds an error.86exp fn is_err<T, E>(self result: &Result<T, E>) -> bool => result is ::Err;8788// The value, or `None` for an error. The value is returned as a copy89// (deep, when T owns heap), as `Option::unwrap_or` returns its payload.90//91// ```alloy92// const done: Result<u32, u8> = ::Ok(3);93// const failed: Result<u32, u8> = ::Err(1);94// expect_equal(&done.ok().unwrap_or(0), &3);95// expect(failed.ok().is_none());96// ```97exp fn ok<T, E>(self result: &Result<T, E>) -> Option<T> {98    if (result is ::Ok |&value|) {99        return ::Some(copy value);100    }101    return ::None;102}103104// The error, or `None` for a value, as a copy like `ok`'s.105exp fn err<T, E>(self result: &Result<T, E>) -> Option<E> {106    if (result is ::Err |&error|) {107        return ::Some(copy error);108    }109    return ::None;110}