You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Similar to Results, but has some additional considerations. For results, we have the Ok functions for each type, and we define "error functions", and a toXResult for errors. For Options, we have Some (similar to Ok), but we don't have errors. So we probably need a None function that returns a Pointer (or some other special None type just to be able to define functions for it) and the corresponding toXOption functions for it.
Preliminary implementation:
enum OptionType {
None,
Some
}
type Bytes32Optionisbytes32;
type AddressOptionisbytes32;
type BoolOptionisbytes32;
libraryLibOptionPointer {
function decode(Pointer self) internalpurereturns (OptionType resultType, Pointer ptr) {
bytesmemory data;
assembly {
data := self
}
(resultType, ptr) =abi.decode(data, (OptionType, Pointer));
}
function isNone(Pointer self) internalpurereturns (bool) {
(OptionType _type,) =decode(self);
return _type == OptionType.None;
}
function isSome(Pointer self) internalpurereturns (bool) {
(OptionType _type,) =decode(self);
return _type == OptionType.Some;
}
function toValue(Pointer self) internalpurereturns (bytes32) {
(, Pointer ptr) =decode(self);
return ptr.asBytes32();
}
function unwrap(Pointer self) internalpurereturns (Pointer ptr) {
if (isNone(self)) {
revert("called `Option::unwrap()` on a `None` value");
}
(, ptr) =decode(self);
}
function expect(Pointer self, stringmemoryerr) internalpurereturns (Pointer ptr) {
if (isNone(self)) {
revert(err);
}
(, ptr) =decode(self);
}
function asBoolOption(Pointer self) internalpurereturns (BoolOption ptr) {
assembly {
ptr := self
}
}
}
libraryLibBytes32Option {
function isNone(Bytes32Option self) internalpurereturns (bool) {
return LibOptionPointer.isNone(self.toPointer());
}
function isSome(Bytes32Option self) internalpurereturns (bool) {
return LibOptionPointer.isSome(self.toPointer());
}
function toValue(Bytes32Option self) internalpurereturns (bytes32) {
(, Pointer ptr) = LibOptionPointer.decode(self.toPointer());
return ptr.asBytes32();
}
function unwrap(Bytes32Option self) internalpurereturns (bytes32) {
return LibOptionPointer.unwrap(self.toPointer()).asBytes32();
}
function expect(Bytes32Option self, stringmemoryerr) internalpurereturns (bytes32) {
return LibOptionPointer.expect(self.toPointer(), err).asBytes32();
}
function toPointer(Bytes32Option self) internalpurereturns (Pointer) {
return Pointer.wrap(Bytes32Option.unwrap(self));
}
}
libraryLibAddressOption {
function isNone(AddressOption self) internalpurereturns (bool) {
return LibOptionPointer.isNone(self.toPointer());
}
function isSome(AddressOption self) internalpurereturns (bool) {
return LibOptionPointer.isSome(self.toPointer());
}
function toValue(AddressOption self) internalpurereturns (address) {
(, Pointer ptr) = LibOptionPointer.decode(self.toPointer());
return ptr.asAddress();
}
function unwrap(AddressOption self) internalpurereturns (address) {
return LibOptionPointer.unwrap(self.toPointer()).asAddress();
}
function expect(AddressOption self, stringmemoryerr) internalpurereturns (address) {
return LibOptionPointer.expect(self.toPointer(), err).asAddress();
}
function toPointer(AddressOption self) internalpurereturns (Pointer) {
return Pointer.wrap(AddressOption.unwrap(self));
}
}
libraryLibBoolOption {
function isNone(BoolOption self) internalpurereturns (bool) {
return LibOptionPointer.isNone(self.toPointer());
}
function isSome(BoolOption self) internalpurereturns (bool) {
return LibOptionPointer.isSome(self.toPointer());
}
function toValue(BoolOption self) internalpurereturns (bool) {
(, Pointer ptr) = LibOptionPointer.decode(self.toPointer());
return ptr.asBool();
}
function unwrap(BoolOption self) internalpurereturns (bool) {
return LibOptionPointer.unwrap(self.toPointer()).asBool();
}
function expect(BoolOption self, stringmemoryerr) internalpurereturns (bool) {
return LibOptionPointer.expect(self.toPointer(), err).asBool();
}
function toPointer(BoolOption self) internalpurereturns (Pointer) {
return Pointer.wrap(BoolOption.unwrap(self));
}
}
function encode(OptionType _type, Pointer _data) purereturns (Pointer result) {
bytesmemory data =abi.encode(_type, _data);
assembly {
result := data
}
}
function None() purereturns (Pointer) {
returnencode(OptionType.None, Pointer.wrap(0));
}
function Some(boolvalue) purereturns (BoolOption opt) {
Pointer _value;
assembly {
_value := value
}
return BoolOption.wrap(Pointer.unwrap(encode(OptionType.Some, _value)));
}
using LibBoolOptionfor BoolOption global;
using LibAddressOptionfor AddressOption global;
using LibBytes32Optionfor Bytes32Option global;
The text was updated successfully, but these errors were encountered:
Similar to Results, but has some additional considerations. For results, we have the
Ok
functions for each type, and we define "error functions", and atoXResult
for errors. For Options, we haveSome
(similar toOk
), but we don't have errors. So we probably need aNone
function that returns a Pointer (or some other special None type just to be able to define functions for it) and the correspondingtoXOption
functions for it.Preliminary implementation:
The text was updated successfully, but these errors were encountered: