Something went wrong. Try again.
An experiment in static, refcounted, procedural language with ownership
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123use std.(c, io)fn read*(path: str) io.Result[Array[u8]] = read(&path)fn read*(path: &str) io.Result[Array[u8]] = match File.open(path) { Result.Ok(file) -> file.read-to-end() Result.Err(err) -> Result.Err(err) }fn read-to-str*(path: str) io.Result[str] = read-to-str(&path)fn read-to-str*(path: &str) io.Result[str] = match File.open(path) { Result.Ok(file) -> file.read-to-str() Result.Err(err) -> Result.Err(err) }fn write*(path: str, contents: str) io.Result[unit] = write(&path, &contents)fn write*(path: &str, contents: str) io.Result[unit] = write(path, &contents)fn write*(path: str, contents: &str) io.Result[unit] = write(&path, contents.as-bytes())fn write*(path: &str, contents: &str) io.Result[unit] = write(path, contents.as-bytes())fn write*(path: str, bytes: &[]u8) io.Result[unit] = write(&path, bytes)fn write*(path: &str, bytes: &[]u8) io.Result[unit] = match File.create(path) { Result.Ok(mut file) -> file.write(bytes).map fn(_) {} Result.Err(err) -> Result.Err(err) }fn File.open*(path: &str) io.Result[File] = match open-impl(path, c.O_RDONLY, 0) { -1 -> Result.Err(io.Error.last-os-error()) fd -> Result.Ok(File(fd)) }fn File.create*(path: &str) io.Result[File] = match open-impl(path, c.O_WRONLY | c.O_CREAT | c.O_TRUNC, 0o644) { -1 -> Result.Err(io.Error.last-os-error()) fd -> Result.Ok(File(fd)) }fn open-impl(path: &str, flags: c.c-int, mode: c.mode) c.c-int = { let pathname = unsafe as[_](path.as-bytes().ptr) // TODO: cross-platform open unsafe c.open(pathname, flags, mode)}type RawFd = c.c-inttype File*(fd: RawFd)const DEFAULT-BUF-SIZE: uint = 8 * 1024fn read-to-end*(file: &File) io.Result[Array[u8]] = { // TODO: initialize with-cap to file's size (or 0 if reading size fails) // TODO: implement `Array.filled(T, uint) where T: Copy` let mut buf = Array.new[u8]() // TODO: implement `std.slice.filled(T, uint) where T: Copy` let mut scratch: []u8 = [:DEFAULT-BUF-SIZE] unsafe scratch.len = DEFAULT-BUF-SIZE for { match file.read(&mut scratch) { Result.Ok(0) -> break Result.Ok(len) -> { // TODO: Implement `std.array.extend([]T)` buf.reserve(len) let mut i = 0 for if i < len { buf.push-unchecked(scratch.[i]) i += 1 } } Result.Err(err) -> return Result.Err(err) } } Result.Ok(buf)}fn read-to-str*(file: &File) io.Result[str] = match file.read-to-end() { Result.Ok(buf) -> match str.from-utf8(buf.take()) { Result.Ok(s) -> Result.Ok(s) Result.Err({}) -> Result.Err(io.Error.InvalidData("stream did not contain valid UTF-8")) } Result.Err(err) -> Result.Err(err) }fn read*(file: &File, buf: &mut []u8) io.Result[uint] = match unsafe c.read(file.fd, buf.ptr, buf.len) { -1 -> Result.Err(io.Error.last-os-error()) len -> Result.Ok(uint(len)) }fn write*(file: &mut File, buf: &[]u8) io.Result[uint] = match unsafe c.write(file.fd, buf.ptr, buf.len) { -1 -> Result.Err(io.Error.last-os-error()) written -> Result.Ok(uint(written)) }fn remove-file*(path: &str) io.Result[unit] = match unsafe c.unlink(as[_](path.ptr)) { -1 -> Result.Err(io.Error.last-os-error()) _ -> Result.Ok({}) }fn `=drop`*(file: &mut File) unit = unsafe c.close(file.fd)