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

context: update ValueContext to handle Any value and more Key types, use closures (#11993)

This commit is contained in:
Ulises Jeremias Cornejo Fandos
2021-09-27 11:52:20 -03:00
committed by GitHub
parent d6a4bce2ef
commit c151e075e1
11 changed files with 131 additions and 90 deletions

View File

@@ -1,6 +1,6 @@
// This module defines the Context type, which carries deadlines, cancellation signals,
// and other request-scoped values across API boundaries and between processes.
// Based off: https://github.com/golang/go/tree/master/src/context
// Based on: https://github.com/golang/go/tree/master/src/context
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
module context
@@ -9,8 +9,8 @@ import time
// A ValueContext carries a key-value pair. It implements Value for that key and
// delegates all other calls to the embedded Context.
pub struct ValueContext {
key string
value voidptr
key Key
value Any
mut:
context Context
}
@@ -25,7 +25,7 @@ mut:
// string or any other built-in type to avoid collisions between
// packages using context. Users of with_value should define their own
// types for keys
pub fn with_value(parent Context, key string, value voidptr) Context {
pub fn with_value(parent Context, key Key, value Any) Context {
return &ValueContext{
context: parent
key: key
@@ -33,25 +33,25 @@ pub fn with_value(parent Context, key string, value voidptr) Context {
}
}
pub fn (ctx ValueContext) deadline() ?time.Time {
pub fn (ctx &ValueContext) deadline() ?time.Time {
return ctx.context.deadline()
}
pub fn (ctx ValueContext) done() chan int {
pub fn (ctx &ValueContext) done() chan int {
return ctx.context.done()
}
pub fn (ctx ValueContext) err() IError {
pub fn (ctx &ValueContext) err() IError {
return ctx.context.err()
}
pub fn (ctx ValueContext) value(key string) ?voidptr {
pub fn (ctx &ValueContext) value(key Key) ?Any {
if ctx.key == key {
return ctx.value
}
return ctx.context.value(key)
}
pub fn (ctx ValueContext) str() string {
pub fn (ctx &ValueContext) str() string {
return context_name(ctx.context) + '.with_value'
}