generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15b9d8d
commit ff0f1ea
Showing
4 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package rx | ||
|
||
// Envelope wraps a struct type T so that it can be defined with | ||
// pointer receivers. It also means that the client does not need | ||
// to manually defined methods for constraint ProxyField as they | ||
// are implemented by the Envelope. | ||
type Envelope[T any, O Numeric] struct { | ||
T *T | ||
P O | ||
} | ||
|
||
// Seal wraps a struct T inside an Envelope | ||
func Seal[T any, O Numeric](t *T) *Envelope[T, O] { | ||
// When using rx, we must instantiate it with a struct, not a pointer | ||
// to struct. So we create a distinction between what is the unit of | ||
// transfer (Envelope) and it's payload of type T. This means that the | ||
// Envelope can only have non pointer receivers, but T can | ||
// be defined with pointer receivers. | ||
// | ||
return &Envelope[T, O]{ | ||
T: t, | ||
} | ||
} | ||
|
||
// Field nominates which member of T of type O is the proxy field required | ||
// to satisfy constraint ProxyField. | ||
func (e Envelope[T, O]) Field() O { | ||
return e.P | ||
} | ||
|
||
// Inc increments the P member of the Envelope required to satisfy constraint ProxyField. | ||
func (e Envelope[T, O]) Inc(index *Envelope[T, O], by Envelope[T, O]) *Envelope[T, O] { | ||
index.P += by.P | ||
|
||
return index | ||
} | ||
|
||
// Index creates a new Envelope from the numeric value of i, required to | ||
// satisfy constraint ProxyField. | ||
func (e Envelope[T, O]) Index(i int) *Envelope[T, O] { | ||
return &Envelope[T, O]{ | ||
P: O(i), | ||
} | ||
} |
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