-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addresses #27
Conversation
pub fn into_inner(self) -> Vec<u8> { | ||
self.0 | ||
} | ||
pub type AccAddress = BaseAddress<0>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newtype pattern instead of simple type alias?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What benefits do we get from using the Newtype pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub struct BaseAddress<const PREFIX: u8>(Vec<u8>);
pub struct AccAddress( BaseAddress<0> );
impl BaseAddress
{
pub fn from_bech32(address: &str, prefix : u32 ) -> Result<Self, AddressError> {
let (hrp, data, variant) = bech32::decode(address)?;
// This code could be removed
//let prefix = if PREFIX == 0 {
// BECH_32_PREFIX_ACC_ADDR
// } else {
// BECH_32_PREFIX_VAL_ADDR
// };
if hrp != prefix {
return Err(AddressError::InvalidPrefix {
expected: prefix.into(),
found: hrp,
});
};
// I omitted other code
}
impl AccAddress
{
pub fn from_bech32(address: &str, ) -> Result<Self, AddressError> {
Self ( BaseAddress::from_bech32( address, BECH_32_PREFIX_ACC_ADDR )
}
}
Unsure about other parts of code, but this one method may be simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would need to do the same for the ValAddress and for the other public methods of AccAddress.
Once more complex const parameter types arrive in Rust, we'll be able to replace u8
with &'static str
, see rust-lang/rust#95174. This will allow for much cleaner code, something like:
pub type AccAddress = BaseAddress<BECH_32_PREFIX_ACC_ADDR>;
pub type ValAddress = BaseAddress<BECH_32_PREFIX_VAL_ADDR>;
#[derive(Debug, PartialEq, Clone)]
pub struct BaseAddress<const PREFIX: &'static str>(Vec<u8>);
impl<const PREFIX: &'static str> BaseAddress<PREFIX> {
pub fn from_bech32(address: &str) -> Result<Self, AddressError> {
let (hrp, data, variant) = bech32::decode(address)?;
// This could be removed
// let prefix = if PREFIX == 0 {
// BECH_32_PREFIX_ACC_ADDR
// } else {
// BECH_32_PREFIX_VAL_ADDR
// };
if hrp != PREFIX {
return Err(AddressError::InvalidPrefix {
expected: PREFIX.into(),
found: hrp,
});
};
I guess there's two questions:
- Do we prefer the future const generic method using
str
or the newtype method? - In the meantime which method should we use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub type AccAddress = BaseAddress<BECH_32_PREFIX_ACC_ADDR>;
pub type ValAddress = BaseAddress<BECH_32_PREFIX_VAL_ADDR>;
Looks much better. I didn't think about it.
- Depends on how quick this feature will arrive and MSV policy.
- Is it really important to use
str
over[u8]
? If yes, then newtype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should use const generics even though we are forced to use a u8
No description provided.