fn: add OptionFromPtr function

Add a function `OptionFromPtr` that constructs an `Option` from a
pointer. The function signature is: `OptionFromPtr : *A -> Option[A]`.

This utility has proven useful in the taproot-assets project.
This commit is contained in:
ffranr 2024-10-25 16:29:16 +01:00
parent acbb33bb7b
commit c49d038540
No known key found for this signature in database
GPG Key ID: B1F8848557AA29D2

View File

@ -26,6 +26,17 @@ func None[A any]() Option[A] {
return Option[A]{}
}
// OptionFromPtr constructs an option from a pointer.
//
// OptionFromPtr : *A -> Option[A].
func OptionFromPtr[A any](a *A) Option[A] {
if a == nil {
return None[A]()
}
return Some[A](*a)
}
// ElimOption is the universal Option eliminator. It can be used to safely
// handle all possible values inside the Option by supplying two continuations.
//