Source file test/typeparam/issue50561.dir/diameter.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package diameter
     6  
     7  type Runnable interface {
     8  	Run()
     9  }
    10  
    11  // RunnableFunc is converter which converts function to Runnable interface
    12  type RunnableFunc func()
    13  
    14  // Run is Runnable.Run
    15  func (r RunnableFunc) Run() {
    16  	r()
    17  }
    18  
    19  type Executor interface {
    20  	ExecuteUnsafe(runnable Runnable)
    21  }
    22  
    23  type Promise[T any] interface {
    24  	Future() Future[T]
    25  	Success(value T) bool
    26  	Failure(err error) bool
    27  	IsCompleted() bool
    28  	Complete(result Try[T]) bool
    29  }
    30  
    31  type Future[T any] interface {
    32  	OnFailure(cb func(err error), ctx ...Executor)
    33  	OnSuccess(cb func(success T), ctx ...Executor)
    34  	Foreach(f func(v T), ctx ...Executor)
    35  	OnComplete(cb func(try Try[T]), ctx ...Executor)
    36  	IsCompleted() bool
    37  	//	Value() Option[Try[T]]
    38  	Failed() Future[error]
    39  	Recover(f func(err error) T, ctx ...Executor) Future[T]
    40  	RecoverWith(f func(err error) Future[T], ctx ...Executor) Future[T]
    41  }
    42  
    43  type Try[T any] struct {
    44  	v   *T
    45  	err error
    46  }
    47  
    48  func (r Try[T]) IsSuccess() bool {
    49  	return r.v != nil
    50  }
    51  
    52  type ByteBuffer struct {
    53  	pos       int
    54  	buf       []byte
    55  	underflow error
    56  }
    57  
    58  // InboundHandler is extends of uclient.NetInboundHandler
    59  type InboundHandler interface {
    60  	OriginHost() string
    61  	OriginRealm() string
    62  }
    63  
    64  type transactionID struct {
    65  	hopID uint32
    66  	endID uint32
    67  }
    68  
    69  type roundTripper struct {
    70  	promise map[transactionID]Promise[*ByteBuffer]
    71  	host    string
    72  	realm   string
    73  }
    74  
    75  func (r *roundTripper) OriginHost() string {
    76  	return r.host
    77  }
    78  func (r *roundTripper) OriginRealm() string {
    79  	return r.realm
    80  }
    81  
    82  func NewInboundHandler(host string, realm string, productName string) InboundHandler {
    83  	ret := &roundTripper{promise: make(map[transactionID]Promise[*ByteBuffer]), host: host, realm: realm}
    84  
    85  	return ret
    86  }
    87  

View as plain text