v / vlib / context
Raw file | 69 loc (56 sloc) | 1008 bytes | Latest commit hash c151e075e
1// This module defines the Context type, which carries deadlines, cancellation signals,
2// and other request-scoped values across API boundaries and between processes.
3// Based on: https://github.com/golang/go/tree/master/src/context
4// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
5module context
6
7import time
8
9// An EmptyContext is never canceled, has no values.
10pub struct EmptyContext {}
11
12pub fn (ctx &EmptyContext) deadline() ?time.Time {
13 return none
14}
15
16pub fn (ctx &EmptyContext) done() chan int {
17 ch := chan int{}
18 ch.close()
19 return ch
20}
21
22pub fn (ctx &EmptyContext) err() IError {
23 return none
24}
25
26pub fn (ctx &EmptyContext) value(key Key) ?Any {
27 return none
28}
29
30pub fn (ctx &EmptyContext) str() string {
31 return 'unknown empty Context'
32}
33
34// A BackgroundContext is never canceled, has no values.
35struct BackgroundContext {
36 EmptyContext
37}
38
39// str returns a string describing the Context.
40pub fn (ctx &BackgroundContext) str() string {
41 return 'context.Background'
42}
43
44// background returns an empty Context. It is never canceled, has no
45// values, and has no deadline. It is typically used by the main function,
46// initialization, and tests, and as the top-level Context for incoming
47// requests.
48pub fn background() Context {
49 return &BackgroundContext{}
50}
51
52// A TodoContext is never canceled, has no values, and has no deadline. It is
53// never used for real work.
54struct TodoContext {
55 EmptyContext
56}
57
58// str returns a string describing the Context.
59pub fn (ctx &TodoContext) str() string {
60 return 'context.TODO'
61}
62
63// todo returns an empty Context. Code should use todo when
64// it's unclear which Context to use or it is not yet available (because the
65// surrounding function has not yet been extended to accept a Context
66// parameter).
67pub fn todo() Context {
68 return &TodoContext{}
69}