-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement custom encoders/decoders
This PR adds the ability to use free functions as custom encoders/decoders. This is important because we want to implement encoding for foreign types (for example netip.*). Signed-off-by: Dmitriy Matrenichev <[email protected]>
- Loading branch information
Showing
6 changed files
with
302 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package protoenc | ||
|
||
//TODO: remove this once Go 1.19 lands | ||
|
||
import ( | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
// A Pointer is an atomic pointer of type *T. The zero value is a nil *T. | ||
type Pointer[T any] struct { | ||
v unsafe.Pointer | ||
} | ||
|
||
// Load atomically loads and returns the value stored in x. | ||
func (x *Pointer[T]) Load() *T { return (*T)(atomic.LoadPointer(&x.v)) } | ||
|
||
// CompareAndSwap executes the compare-and-swap operation for x. | ||
func (x *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool) { | ||
return atomic.CompareAndSwapPointer(&x.v, unsafe.Pointer(old), unsafe.Pointer(new)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters