Skip to content

Commit 963e3e8

Browse files
authored
builtin: small cleanup and merging of several small files into vlib/builtin/chan_option_result.v (#25089)
1 parent 3507477 commit 963e3e8

File tree

5 files changed

+204
-217
lines changed

5 files changed

+204
-217
lines changed

vlib/builtin/chan.v

Lines changed: 0 additions & 32 deletions
This file was deleted.

vlib/builtin/chan_option_result.v

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
module builtin
2+
3+
// ChanState describes the result of an attempted channel transaction.
4+
pub enum ChanState {
5+
success
6+
not_ready // push()/pop() would have to wait, but no_block was requested
7+
closed
8+
}
9+
10+
/*
11+
The following methods are only stubs.
12+
The real implementation is in `vlib/sync/channels.v`
13+
*/
14+
15+
// close closes the channel for further push transactions.
16+
// closed channels cannot be pushed to, however they can be popped
17+
// from as long as there is still objects available in the channel buffer.
18+
pub fn (ch chan) close() {}
19+
20+
// try_pop returns `ChanState.success` if an object is popped from the channel.
21+
// try_pop effectively pops from the channel without waiting for objects to become available.
22+
// Both the test and pop transaction is done atomically.
23+
pub fn (ch chan) try_pop(obj voidptr) ChanState {
24+
return .success
25+
}
26+
27+
// try_push returns `ChanState.success` if the object is pushed to the channel.
28+
// try_push effectively both push and test if the transaction `ch <- a` succeeded.
29+
// Both the test and push transaction is done atomically.
30+
pub fn (ch chan) try_push(obj voidptr) ChanState {
31+
return .success
32+
}
33+
34+
// IError holds information about an error instance.
35+
pub interface IError {
36+
msg() string
37+
code() int
38+
}
39+
40+
struct _result {
41+
is_error bool
42+
err IError = none__
43+
// Data is trailing after err
44+
// and is not included in here but in the
45+
// derived Result_xxx types
46+
}
47+
48+
fn _result_ok(data voidptr, mut res _result, size int) {
49+
unsafe {
50+
*res = _result{}
51+
// use err to get the end of ResultBase and then memcpy into it
52+
vmemcpy(&u8(&res.err) + sizeof(IError), data, size)
53+
}
54+
}
55+
56+
// str returns the message of IError.
57+
pub fn (err IError) str() string {
58+
return match err {
59+
None__ {
60+
'none'
61+
}
62+
Error {
63+
err.msg()
64+
}
65+
MessageError {
66+
(*err).str()
67+
}
68+
else {
69+
'${err.type_name()}: ${err.msg()}'
70+
}
71+
}
72+
}
73+
74+
// Error is the empty default implementation of `IError`.
75+
pub struct Error {}
76+
77+
pub fn (err Error) msg() string {
78+
return ''
79+
}
80+
81+
pub fn (err Error) code() int {
82+
return 0
83+
}
84+
85+
// MessageError is the default implementation of the `IError` interface that is returned by the `error()` function.
86+
struct MessageError {
87+
pub:
88+
msg string
89+
code int
90+
}
91+
92+
// str returns both the .msg and .code of MessageError, when .code is != 0 .
93+
pub fn (err MessageError) str() string {
94+
if err.code > 0 {
95+
return '${err.msg}; code: ${err.code}'
96+
}
97+
return err.msg
98+
}
99+
100+
// msg returns only the message of MessageError.
101+
pub fn (err MessageError) msg() string {
102+
return err.msg
103+
}
104+
105+
// code returns only the code of MessageError.
106+
pub fn (err MessageError) code() int {
107+
return err.code
108+
}
109+
110+
@[unsafe]
111+
pub fn (err &MessageError) free() {
112+
unsafe { err.msg.free() }
113+
}
114+
115+
@[if trace_error ?]
116+
fn trace_error(x string) {
117+
eprintln('> ${@FN} | ${x}')
118+
}
119+
120+
// error returns a default error instance containing the error given in `message`.
121+
// Example: if ouch { return error('an error occurred') }
122+
@[inline]
123+
pub fn error(message string) IError {
124+
trace_error(message)
125+
return &MessageError{
126+
msg: message
127+
}
128+
}
129+
130+
// error_with_code returns a default error instance containing the given `message` and error `code`.
131+
// Example: if ouch { return error_with_code('an error occurred', 1) }
132+
@[inline]
133+
pub fn error_with_code(message string, code int) IError {
134+
trace_error('${message} | code: ${code}')
135+
return &MessageError{
136+
msg: message
137+
code: code
138+
}
139+
}
140+
141+
// Option is the base of V's internal option return system.
142+
struct Option {
143+
state u8 // 0 - ok; 2 - none; 1 - ?
144+
err IError = none__
145+
// Data is trailing after err
146+
// and is not included in here but in the
147+
// derived Option_xxx types
148+
}
149+
150+
// option is the base of V's internal option return system.
151+
struct _option {
152+
state u8
153+
err IError = none__
154+
// Data is trailing after err
155+
// and is not included in here but in the
156+
// derived _option_xxx types
157+
}
158+
159+
fn _option_none(data voidptr, mut option _option, size int) {
160+
unsafe {
161+
*option = _option{
162+
state: 2
163+
}
164+
// use err to get the end of OptionBase and then memcpy into it
165+
vmemcpy(&u8(&option.err) + sizeof(IError), data, size)
166+
}
167+
}
168+
169+
fn _option_ok(data voidptr, mut option _option, size int) {
170+
unsafe {
171+
*option = _option{}
172+
// use err to get the end of OptionBase and then memcpy into it
173+
vmemcpy(&u8(&option.err) + sizeof(IError), data, size)
174+
}
175+
}
176+
177+
fn _option_clone(current &_option, mut option _option, size int) {
178+
unsafe {
179+
*option = _option{
180+
state: current.state
181+
err: current.err
182+
}
183+
// use err to get the end of OptionBase and then memcpy into it
184+
vmemcpy(&u8(&option.err) + sizeof(IError), &u8(&current.err) + sizeof(IError),
185+
size)
186+
}
187+
}
188+
189+
//
190+
191+
const none__ = IError(&None__{})
192+
193+
struct None__ {
194+
Error
195+
}
196+
197+
fn (_ None__) str() string {
198+
return 'none'
199+
}
200+
201+
// str for none, returns 'none'
202+
pub fn (_ none) str() string {
203+
return 'none'
204+
}

vlib/builtin/float_x64.v

Lines changed: 0 additions & 6 deletions
This file was deleted.

vlib/builtin/option.v

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)