Skip to content

Commit fae46a8

Browse files
committed
builtin: extract vlib/builtin/result.v from vlib/builtin/option.v
1 parent 7891053 commit fae46a8

File tree

2 files changed

+125
-116
lines changed

2 files changed

+125
-116
lines changed

vlib/builtin/option.v

Lines changed: 10 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,6 @@
33
// that can be found in the LICENSE file.
44
module builtin
55

6-
// IError holds information about an error instance
7-
pub interface IError {
8-
// >> Hack to allow old style custom error implementations
9-
// TODO: remove once deprecation period for `IError` methods has ended
10-
msg string
11-
code int // <<
12-
msg() string
13-
code() int
14-
}
15-
16-
// str returns the message of IError
17-
pub fn (err IError) str() string {
18-
return match err {
19-
None__ {
20-
'none'
21-
}
22-
Error {
23-
err.msg()
24-
}
25-
MessageError {
26-
err.msg()
27-
}
28-
else {
29-
// >> Hack to allow old style custom error implementations
30-
// TODO: remove once deprecation period for `IError` methods has ended
31-
// old_error_style := unsafe { voidptr(&err.msg) != voidptr(&err.code) } // if fields are not defined (new style) they don't have an offset between
32-
// <<
33-
'${err.type_name()}: ${err.msg()}'
34-
}
35-
}
36-
}
37-
38-
// Error is the empty default implementation of `IError`.
39-
pub struct Error {}
40-
41-
pub fn (err Error) msg() string {
42-
return ''
43-
}
44-
45-
pub fn (err Error) code() int {
46-
return 0
47-
}
48-
49-
// MessageError is the default implementation of the `IError` interface that is returned by the `error()` function
50-
struct MessageError {
51-
pub:
52-
msg string
53-
code int
54-
}
55-
56-
// msg returns the message of MessageError
57-
pub fn (err MessageError) msg() string {
58-
if err.code > 0 {
59-
return '${err.msg}; code: ${err.code}'
60-
}
61-
return err.msg
62-
}
63-
64-
// code returns the code of MessageError
65-
pub fn (err MessageError) code() int {
66-
return err.code
67-
}
68-
69-
[unsafe]
70-
pub fn (err &MessageError) free() {
71-
unsafe { err.msg.free() }
72-
}
73-
74-
const none__ = IError(&None__{})
75-
76-
struct None__ {
77-
Error
78-
}
79-
80-
fn (_ None__) str() string {
81-
return 'none'
82-
}
83-
84-
[if trace_error ?]
85-
fn trace_error(x string) {
86-
eprintln('> ${@FN} | ${x}')
87-
}
88-
89-
// error returns a default error instance containing the error given in `message`.
90-
// Example: if ouch { return error('an error occurred') }
91-
[inline]
92-
pub fn error(message string) IError {
93-
trace_error(message)
94-
return &MessageError{
95-
msg: message
96-
}
97-
}
98-
99-
// error_with_code returns a default error instance containing the given `message` and error `code`.
100-
// Example: if ouch { return error_with_code('an error occurred', 1) }
101-
[inline]
102-
pub fn error_with_code(message string, code int) IError {
103-
trace_error('${message} | code: ${code}')
104-
return &MessageError{
105-
msg: message
106-
code: code
107-
}
108-
}
109-
1106
// Option is the base of V's internal option return system.
1117
struct Option {
1128
state u8
@@ -125,6 +21,7 @@ struct _option {
12521
// derived _option_xxx types
12622
}
12723

24+
[markused]
12825
fn _option_none(data voidptr, mut option _option, size int) {
12926
unsafe {
13027
*option = _option{
@@ -135,6 +32,7 @@ fn _option_none(data voidptr, mut option _option, size int) {
13532
}
13633
}
13734

35+
[markused]
13836
fn _option_ok(data voidptr, mut option _option, size int) {
13937
unsafe {
14038
*option = _option{}
@@ -143,20 +41,16 @@ fn _option_ok(data voidptr, mut option _option, size int) {
14341
}
14442
}
14543

146-
struct _result {
147-
is_error bool
148-
err IError = none__
149-
// Data is trailing after err
150-
// and is not included in here but in the
151-
// derived Result_xxx types
44+
//
45+
46+
const none__ = IError(&None__{})
47+
48+
struct None__ {
49+
Error
15250
}
15351

154-
fn _result_ok(data voidptr, mut res _result, size int) {
155-
unsafe {
156-
*res = _result{}
157-
// use err to get the end of ResultBase and then memcpy into it
158-
vmemcpy(&u8(&res.err) + sizeof(IError), data, size)
159-
}
52+
fn (_ None__) str() string {
53+
return 'none'
16054
}
16155

16256
pub fn (_ none) str() string {

vlib/builtin/result.v

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by an MIT license
3+
// that can be found in the LICENSE file.
4+
module builtin
5+
6+
// IError holds information about an error instance
7+
pub interface IError {
8+
// >> Hack to allow old style custom error implementations
9+
// TODO: remove once deprecation period for `IError` methods has ended
10+
msg string
11+
code int // <<
12+
msg() string
13+
code() int
14+
}
15+
16+
struct _result {
17+
is_error bool
18+
err IError = none__
19+
// Data is trailing after err
20+
// and is not included in here but in the
21+
// derived Result_xxx types
22+
}
23+
24+
[markused]
25+
fn _result_ok(data voidptr, mut res _result, size int) {
26+
unsafe {
27+
*res = _result{}
28+
// use err to get the end of ResultBase and then memcpy into it
29+
vmemcpy(&u8(&res.err) + sizeof(IError), data, size)
30+
}
31+
}
32+
33+
// str returns the message of IError
34+
pub fn (err IError) str() string {
35+
return match err {
36+
None__ {
37+
'none'
38+
}
39+
Error {
40+
err.msg()
41+
}
42+
MessageError {
43+
err.msg()
44+
}
45+
else {
46+
// >> Hack to allow old style custom error implementations
47+
// TODO: remove once deprecation period for `IError` methods has ended
48+
// old_error_style := unsafe { voidptr(&err.msg) != voidptr(&err.code) } // if fields are not defined (new style) they don't have an offset between
49+
// <<
50+
'${err.type_name()}: ${err.msg()}'
51+
}
52+
}
53+
}
54+
55+
// Error is the empty default implementation of `IError`.
56+
pub struct Error {}
57+
58+
pub fn (err Error) msg() string {
59+
return ''
60+
}
61+
62+
pub fn (err Error) code() int {
63+
return 0
64+
}
65+
66+
// MessageError is the default implementation of the `IError` interface that is returned by the `error()` function
67+
struct MessageError {
68+
pub:
69+
msg string
70+
code int
71+
}
72+
73+
// msg returns the message of MessageError
74+
pub fn (err MessageError) msg() string {
75+
if err.code > 0 {
76+
return '${err.msg}; code: ${err.code}'
77+
}
78+
return err.msg
79+
}
80+
81+
// code returns the code of MessageError
82+
pub fn (err MessageError) code() int {
83+
return err.code
84+
}
85+
86+
[unsafe]
87+
pub fn (err &MessageError) free() {
88+
unsafe { err.msg.free() }
89+
}
90+
91+
[if trace_error ?]
92+
fn trace_error(x string) {
93+
eprintln('> ${@FN} | ${x}')
94+
}
95+
96+
// error returns a default error instance containing the error given in `message`.
97+
// Example: if ouch { return error('an error occurred') }
98+
[inline]
99+
pub fn error(message string) IError {
100+
trace_error(message)
101+
return &MessageError{
102+
msg: message
103+
}
104+
}
105+
106+
// error_with_code returns a default error instance containing the given `message` and error `code`.
107+
// Example: if ouch { return error_with_code('an error occurred', 1) }
108+
[inline]
109+
pub fn error_with_code(message string, code int) IError {
110+
trace_error('${message} | code: ${code}')
111+
return &MessageError{
112+
msg: message
113+
code: code
114+
}
115+
}

0 commit comments

Comments
 (0)