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

os: clean up file functions, use optionals

This commit is contained in:
Alexander Medvednikov
2019-07-03 21:07:42 +02:00
parent ffb4da791d
commit dec0d961f5
8 changed files with 107 additions and 64 deletions

View File

@@ -94,23 +94,11 @@ fn _strlen(s byteptr) int {
return C.strlen(s)
}
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr) Option {
return Option {
data: data
ok: true
}
}
fn memdup(src voidptr, sz int) voidptr {
mem := malloc(sz)
return C.memcpy(mem, src, sz)
}
pub fn error(s string) Option {
return Option {
error: s
}
}

View File

@@ -5,8 +5,27 @@
module builtin
struct Option {
data voidptr
data [255] byte
error string
ok bool
}
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr, size int) Option {
if size > 255 {
panic('option size too big: $size (max is 255)')
}
res := Option {
ok: true
}
C.memcpy(res.data, data, size)
return res
}
pub fn error(s string) Option {
return Option {
error: s
}
}