1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: change optional to result of io (#16075)

This commit is contained in:
yuyi
2022-10-16 14:28:57 +08:00
committed by GitHub
parent 6e46933c55
commit f6844e9766
187 changed files with 1885 additions and 1874 deletions

View File

@ -1,6 +1,6 @@
module notify
// Implement the API
pub fn new() ?FdNotifier {
pub fn new() !FdNotifier {
panic('unsupported')
}

View File

@ -43,7 +43,7 @@ pub:
// new creates a new EpollNotifier
// The FdNotifier interface is returned to allow OS specific
// implementations without exposing the concrete type
pub fn new() ?FdNotifier {
pub fn new() !FdNotifier {
fd := C.epoll_create1(0) // 0 indicates default behavior
if fd == -1 {
return error(os.posix_get_error_msg(C.errno))
@ -69,7 +69,7 @@ const (
)
// ctl is a helper method for add, modify, and remove
fn (mut en EpollNotifier) ctl(fd int, op int, mask u32) ? {
fn (mut en EpollNotifier) ctl(fd int, op int, mask u32) ! {
event := C.epoll_event{
events: mask
data: C.epoll_data_t{
@ -82,20 +82,20 @@ fn (mut en EpollNotifier) ctl(fd int, op int, mask u32) ? {
}
// add adds a file descriptor to the watch list
fn (mut en EpollNotifier) add(fd int, events FdEventType, conf ...FdConfigFlags) ? {
fn (mut en EpollNotifier) add(fd int, events FdEventType, conf ...FdConfigFlags) ! {
mask := flags_to_mask(events, ...conf)
en.ctl(fd, C.EPOLL_CTL_ADD, mask)?
en.ctl(fd, C.EPOLL_CTL_ADD, mask)!
}
// modify sets an existing entry in the watch list to the provided events and configuration
fn (mut en EpollNotifier) modify(fd int, events FdEventType, conf ...FdConfigFlags) ? {
fn (mut en EpollNotifier) modify(fd int, events FdEventType, conf ...FdConfigFlags) ! {
mask := flags_to_mask(events, ...conf)
en.ctl(fd, C.EPOLL_CTL_MOD, mask)?
en.ctl(fd, C.EPOLL_CTL_MOD, mask)!
}
// remove removes a file descriptor from the watch list
fn (mut en EpollNotifier) remove(fd int) ? {
en.ctl(fd, C.EPOLL_CTL_DEL, 0)?
fn (mut en EpollNotifier) remove(fd int) ! {
en.ctl(fd, C.EPOLL_CTL_DEL, 0)!
}
// wait waits to be notified of events on the watch list,
@ -133,7 +133,7 @@ fn (mut en EpollNotifier) wait(timeout time.Duration) []FdEvent {
// close closes the EpollNotifier,
// any successive calls to add, modify, remove, and wait should fail
fn (mut en EpollNotifier) close() ? {
fn (mut en EpollNotifier) close() ! {
if C.close(en.epoll_fd) == -1 {
return error(os.posix_get_error_msg(C.errno))
}

View File

@ -5,11 +5,11 @@ import time
// Backends should provide a `new()?FdNotifier` function
pub interface FdNotifier {
mut:
add(fd int, events FdEventType, conf ...FdConfigFlags) ?
modify(fd int, events FdEventType, conf ...FdConfigFlags) ?
remove(fd int) ?
add(fd int, events FdEventType, conf ...FdConfigFlags) !
modify(fd int, events FdEventType, conf ...FdConfigFlags) !
remove(fd int) !
wait(timeout time.Duration) []FdEvent
close() ?
close() !
}
pub interface FdEvent {

View File

@ -4,7 +4,7 @@ import os
import os.notify
// make a pipe and return the (read, write) file descriptors
fn make_pipe() ?(int, int) {
fn make_pipe() !(int, int) {
$if linux {
pipefd := [2]int{}
if C.pipe(&pipefd[0]) != 0 {
@ -18,14 +18,14 @@ fn make_pipe() ?(int, int) {
fn test_level_trigger() {
// currently only linux is supported
$if linux {
mut notifier := notify.new()?
reader, writer := make_pipe()?
mut notifier := notify.new()!
reader, writer := make_pipe()!
defer {
os.fd_close(reader)
os.fd_close(writer)
notifier.close() or {}
}
notifier.add(reader, .read)?
notifier.add(reader, .read)!
os.fd_write(writer, 'foobar')
mut n := &notifier
@ -39,14 +39,14 @@ fn test_level_trigger() {
fn test_edge_trigger() {
// currently only linux is supported
$if linux {
mut notifier := notify.new()?
reader, writer := make_pipe()?
mut notifier := notify.new()!
reader, writer := make_pipe()!
defer {
os.fd_close(reader)
os.fd_close(writer)
notifier.close() or {}
}
notifier.add(reader, .read, .edge_trigger)?
notifier.add(reader, .read, .edge_trigger)!
mut n := &notifier
@ -66,14 +66,14 @@ fn test_edge_trigger() {
fn test_one_shot() {
$if linux {
mut notifier := notify.new()?
reader, writer := make_pipe()?
mut notifier := notify.new()!
reader, writer := make_pipe()!
defer {
os.fd_close(reader)
os.fd_close(writer)
notifier.close() or {}
}
notifier.add(reader, .read, .one_shot)?
notifier.add(reader, .read, .one_shot)!
mut n := &notifier
@ -84,20 +84,20 @@ fn test_one_shot() {
assert notifier.wait(0).len == 0
// rearm
notifier.modify(reader, .read)?
notifier.modify(reader, .read)!
check_read_event(mut n, reader, 'barbaz')
}
}
fn test_hangup() {
$if linux {
mut notifier := notify.new()?
reader, writer := make_pipe()?
mut notifier := notify.new()!
reader, writer := make_pipe()!
defer {
os.fd_close(reader)
notifier.close() or {}
}
notifier.add(reader, .hangup)?
notifier.add(reader, .hangup)!
assert notifier.wait(0).len == 0
@ -113,18 +113,18 @@ fn test_hangup() {
fn test_write() {
$if linux {
mut notifier := notify.new()?
reader, writer := make_pipe()?
mut notifier := notify.new()!
reader, writer := make_pipe()!
defer {
os.fd_close(reader)
os.fd_close(writer)
notifier.close() or {}
}
notifier.add(reader, .write)?
notifier.add(reader, .write)!
assert notifier.wait(0).len == 0
notifier.add(writer, .write)?
notifier.add(writer, .write)!
events := notifier.wait(0)
assert events.len == 1
assert events[0].fd == writer
@ -134,8 +134,8 @@ fn test_write() {
fn test_remove() {
$if linux {
mut notifier := notify.new()?
reader, writer := make_pipe()?
mut notifier := notify.new()!
reader, writer := make_pipe()!
defer {
os.fd_close(reader)
os.fd_close(writer)
@ -144,12 +144,12 @@ fn test_remove() {
// level triggered - will keep getting events while
// there is data to read
notifier.add(reader, .read)?
notifier.add(reader, .read)!
os.fd_write(writer, 'foobar')
assert notifier.wait(0).len == 1
assert notifier.wait(0).len == 1
notifier.remove(reader)?
notifier.remove(reader)!
assert notifier.wait(0).len == 0
}
}