mirror of https://github.com/v2ray/v2ray-core
				
				
				
			
		
			
				
	
	
		
			26 lines
		
	
	
		
			461 B
		
	
	
	
		
			Go
		
	
	
			
		
		
	
	
			26 lines
		
	
	
		
			461 B
		
	
	
	
		
			Go
		
	
	
| package protocol
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| )
 | |
| 
 | |
| type key int
 | |
| 
 | |
| const (
 | |
| 	userKey key = iota
 | |
| )
 | |
| 
 | |
| // ContextWithUser returns a context combined with an User.
 | |
| func ContextWithUser(ctx context.Context, user *User) context.Context {
 | |
| 	return context.WithValue(ctx, userKey, user)
 | |
| }
 | |
| 
 | |
| // UserFromContext extracts an User from the given context, if any.
 | |
| func UserFromContext(ctx context.Context) *User {
 | |
| 	v := ctx.Value(userKey)
 | |
| 	if v == nil {
 | |
| 		return nil
 | |
| 	}
 | |
| 	return v.(*User)
 | |
| }
 |