Skip to content

Commit

Permalink
Privatize CanonicalAddr, Binary and Size inner val
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Sep 15, 2023
1 parent 7b2e753 commit 657cece
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
34 changes: 25 additions & 9 deletions packages/std/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl<'a> From<&'a Addr> for Cow<'a, Addr> {
/// So the type shoud be treated as a marker to express the intended data type, not as
/// a validity guarantee of any sort.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, JsonSchema)]
pub struct CanonicalAddr(pub Binary);
pub struct CanonicalAddr(Binary);

/// Implement `CanonicalAddr == Binary`
impl PartialEq<Binary> for CanonicalAddr {
Expand Down Expand Up @@ -541,26 +541,34 @@ mod tests {
let original_ptr = original.as_ptr();
let addr: CanonicalAddr = original.into();
assert_eq!(addr.as_slice(), [0u8, 187, 61, 11, 250, 0]);
assert_eq!((addr.0).0.as_ptr(), original_ptr, "must not be copied");
assert_eq!(
(addr.0).as_slice().as_ptr(),
original_ptr,
"must not be copied"
);

// From<Vec<u8>> for CanonicalAddr
let original = vec![0u8, 187, 61, 11, 250, 0];
let original_ptr = original.as_ptr();
let addr = CanonicalAddr::from(original);
assert_eq!(addr.as_slice(), [0u8, 187, 61, 11, 250, 0]);
assert_eq!((addr.0).0.as_ptr(), original_ptr, "must not be copied");
assert_eq!(
(addr.0).as_slice().as_ptr(),
original_ptr,
"must not be copied"
);

// Into<Vec<u8>> for CanonicalAddr
// This test is a bit pointless because we get Into from the From implementation
let original = CanonicalAddr::from(vec![0u8, 187, 61, 11, 250, 0]);
let original_ptr = (original.0).0.as_ptr();
let original_ptr = (original.0).as_slice().as_ptr();
let vec: Vec<u8> = original.into();
assert_eq!(vec.as_slice(), [0u8, 187, 61, 11, 250, 0]);
assert_eq!(vec.as_ptr(), original_ptr, "must not be copied");

// From<CanonicalAddr> for Vec<u8>
let original = CanonicalAddr::from(vec![7u8, 35, 49, 101, 0, 255]);
let original_ptr = (original.0).0.as_ptr();
let original_ptr = (original.0).as_slice().as_ptr();
let vec = Vec::<u8>::from(original);
assert_eq!(vec.as_slice(), [7u8, 35, 49, 101, 0, 255]);
assert_eq!(vec.as_ptr(), original_ptr, "must not be copied");
Expand All @@ -573,11 +581,15 @@ mod tests {
let original_ptr = original.as_ptr();
let addr = CanonicalAddr::from(original);
assert_eq!(addr.as_slice(), [0u8, 187, 61, 11, 250, 0]);
assert_eq!((addr.0).0.as_ptr(), original_ptr, "must not be copied");
assert_eq!(
(addr.0).as_slice().as_ptr(),
original_ptr,
"must not be copied"
);

// From<CanonicalAddr> for Binary
let original = CanonicalAddr::from(vec![7u8, 35, 49, 101, 0, 255]);
let original_ptr = (original.0).0.as_ptr();
let original_ptr = (original.0).as_slice().as_ptr();
let bin = Binary::from(original);
assert_eq!(bin.as_slice(), [7u8, 35, 49, 101, 0, 255]);
assert_eq!(bin.as_ptr(), original_ptr, "must not be copied");
Expand All @@ -590,11 +602,15 @@ mod tests {
let original_ptr = original.as_ptr();
let addr = CanonicalAddr::from(original);
assert_eq!(addr.as_slice(), [0u8, 187, 61, 11, 250, 0]);
assert_eq!((addr.0).0.as_ptr(), original_ptr, "must not be copied");
assert_eq!(
(addr.0).as_slice().as_ptr(),
original_ptr,
"must not be copied"
);

// From<CanonicalAddr> for HexBinary
let original = CanonicalAddr::from(vec![7u8, 35, 49, 101, 0, 255]);
let original_ptr = (original.0).0.as_ptr();
let original_ptr = (original.0).as_slice().as_ptr();
let bin = HexBinary::from(original);
assert_eq!(bin.as_slice(), [7u8, 35, 49, 101, 0, 255]);
assert_eq!(bin.as_ptr(), original_ptr, "must not be copied");
Expand Down
7 changes: 6 additions & 1 deletion packages/std/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::errors::{StdError, StdResult};
/// This is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>.
/// See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>.
#[derive(Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord, JsonSchema)]
pub struct Binary(#[schemars(with = "String")] pub Vec<u8>);
pub struct Binary(#[schemars(with = "String")] Vec<u8>);

impl Binary {
/// Base64 encoding engine used in conversion to/from base64.
Expand All @@ -26,6 +26,11 @@ impl Binary {
.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
);

/// Creates a new `Binary` containing the given data.
pub const fn new(data: Vec<u8>) -> Self {
Self(data)
}

/// take an (untrusted) string and decode it into bytes.
/// fails if it is not valid base64
pub fn from_base64(encoded: &str) -> StdResult<Self> {
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ pub struct IbcReceiveResponse<T = Empty> {
impl<T> Default for IbcReceiveResponse<T> {
fn default() -> Self {
IbcReceiveResponse {
acknowledgement: Binary(vec![]),
acknowledgement: Binary::default(),
messages: vec![],
attributes: vec![],
events: vec![],
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn to_binary<T>(data: &T) -> StdResult<Binary>
where
T: Serialize + ?Sized,
{
to_vec(data).map(Binary)
to_vec(data).map(Binary::new)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/size.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[derive(Copy, Clone, Debug)]
pub struct Size(pub usize);
pub struct Size(usize);

impl Size {
/// Creates a size of `n` kilo
Expand Down

0 comments on commit 657cece

Please sign in to comment.