Skip to content

Commit 4553dc3

Browse files
authored
Merge pull request CosmWasm#554 from CosmWasm/convert-into-vec
Add Binary::into_vec and CanonicalAddr::into_vec
2 parents 2648dd0 + 3ab3fbd commit 4553dc3

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
- Create trait `CustomQuery` for the generic argument in
3333
`QueryRequest<C: CustomQuery>`. This allows us to provide
3434
`impl<C: CustomQuery> From<C> for QueryRequest<C>` for any custom query.
35+
- Implement `From<Binary> for Vec<u8>`.
36+
- Implement `From<CanonicalAddr> for Vec<u8>`.
37+
- Add `Binary::into_vec` and `CanonicalAddr::into_vec`.
3538

3639
**cosmwasm-storage**
3740

packages/std/src/addresses.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ impl From<Vec<u8>> for CanonicalAddr {
6767
}
6868
}
6969

70+
impl From<CanonicalAddr> for Vec<u8> {
71+
fn from(source: CanonicalAddr) -> Vec<u8> {
72+
source.0.into()
73+
}
74+
}
75+
7076
impl CanonicalAddr {
7177
pub fn as_slice(&self) -> &[u8] {
7278
&self.0.as_slice()
@@ -79,6 +85,26 @@ impl CanonicalAddr {
7985
pub fn is_empty(&self) -> bool {
8086
self.0.is_empty()
8187
}
88+
89+
/// Converts a `CanonicalAddr` into a vector of bytes.
90+
///
91+
/// This consumes the `CanonicalAddr`, so we do not need to copy its contents.
92+
/// It is equivalent to both `Vec::<u8>::from(addr)` and `let v: Vec<u8> = addr.into()` and just a matter of taste which one you use.
93+
///
94+
/// # Examples
95+
///
96+
/// Basic usage:
97+
///
98+
/// ```
99+
/// # use cosmwasm_std::CanonicalAddr;
100+
/// let address = CanonicalAddr::from(vec![0, 187, 61, 11, 250, 0]);
101+
/// let bytes = address.into_vec();
102+
///
103+
/// assert_eq!(bytes, &[0, 187, 61, 11, 250, 0]);
104+
/// ```
105+
pub fn into_vec(self) -> Vec<u8> {
106+
self.into()
107+
}
82108
}
83109

84110
impl fmt::Display for CanonicalAddr {
@@ -158,6 +184,40 @@ mod test {
158184
assert_eq!(canonical_addr_vec.as_slice(), &[0u8, 187, 61, 11, 250, 0]);
159185
}
160186

187+
#[test]
188+
fn canonical_addr_from_vec_works() {
189+
// Into<CanonicalAddr> for Vec<u8>
190+
let original = vec![0u8, 187, 61, 11, 250, 0];
191+
let original_ptr = original.as_ptr();
192+
let addr: CanonicalAddr = original.into();
193+
assert_eq!(addr.as_slice(), [0u8, 187, 61, 11, 250, 0]);
194+
assert_eq!((addr.0).0.as_ptr(), original_ptr, "must not be copied");
195+
196+
// From<Vec<u8>> for CanonicalAddr
197+
let original = vec![0u8, 187, 61, 11, 250, 0];
198+
let original_ptr = original.as_ptr();
199+
let addr = CanonicalAddr::from(original);
200+
assert_eq!(addr.as_slice(), [0u8, 187, 61, 11, 250, 0]);
201+
assert_eq!((addr.0).0.as_ptr(), original_ptr, "must not be copied");
202+
}
203+
204+
#[test]
205+
fn canonical_addr_into_vec_works() {
206+
// Into<Vec<u8>> for CanonicalAddr
207+
let original = CanonicalAddr::from(vec![0u8, 187, 61, 11, 250, 0]);
208+
let original_ptr = (original.0).0.as_ptr();
209+
let vec: Vec<u8> = original.into();
210+
assert_eq!(vec.as_slice(), [0u8, 187, 61, 11, 250, 0]);
211+
assert_eq!(vec.as_ptr(), original_ptr, "must not be copied");
212+
213+
// From<CanonicalAddr> for Vec<u8>
214+
let original = CanonicalAddr::from(vec![7u8, 35, 49, 101, 0, 255]);
215+
let original_ptr = (original.0).0.as_ptr();
216+
let vec = Vec::<u8>::from(original);
217+
assert_eq!(vec.as_slice(), [7u8, 35, 49, 101, 0, 255]);
218+
assert_eq!(vec.as_ptr(), original_ptr, "must not be copied");
219+
}
220+
161221
#[test]
162222
fn canonical_addr_len() {
163223
let bytes: &[u8] = &[0u8, 187, 61, 11, 250, 0];

packages/std/src/encoding.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,38 @@ impl Binary {
2525
pub fn to_base64(&self) -> String {
2626
base64::encode(&self.0)
2727
}
28+
2829
pub fn as_slice(&self) -> &[u8] {
2930
self.0.as_slice()
3031
}
32+
3133
pub fn len(&self) -> usize {
3234
self.0.len()
3335
}
36+
3437
pub fn is_empty(&self) -> bool {
3538
self.0.is_empty()
3639
}
40+
41+
/// Converts a `Binary` into a vector of bytes.
42+
///
43+
/// This consumes the `Binary`, so we do not need to copy its contents.
44+
/// It is equivalent to both `Vec::<u8>::from(binary)` and `let v: Vec<u8> = binary.into()` and just a matter of taste which one you use.
45+
///
46+
/// # Examples
47+
///
48+
/// Basic usage:
49+
///
50+
/// ```
51+
/// # use cosmwasm_std::Binary;
52+
/// let binary = Binary::from_base64("ALs9C/oA").unwrap();
53+
/// let bytes = binary.into_vec();
54+
///
55+
/// assert_eq!(bytes, &[0, 187, 61, 11, 250, 0]);
56+
/// ```
57+
pub fn into_vec(self) -> Vec<u8> {
58+
self.into()
59+
}
3760
}
3861

3962
impl fmt::Display for Binary {
@@ -85,9 +108,9 @@ impl From<Vec<u8>> for Binary {
85108
}
86109
}
87110

88-
impl Into<Vec<u8>> for Binary {
89-
fn into(self) -> Vec<u8> {
90-
self.0
111+
impl From<Binary> for Vec<u8> {
112+
fn from(original: Binary) -> Vec<u8> {
113+
original.0
91114
}
92115
}
93116

@@ -278,11 +301,19 @@ mod test {
278301

279302
#[test]
280303
fn into_vec_works() {
304+
// Into<Vec<u8>> for Binary
281305
let original = Binary(vec![0u8, 187, 61, 11, 250, 0]);
282306
let original_ptr = original.0.as_ptr();
283307
let vec: Vec<u8> = original.into();
284308
assert_eq!(vec.as_slice(), [0u8, 187, 61, 11, 250, 0]);
285309
assert_eq!(vec.as_ptr(), original_ptr, "vector must not be copied");
310+
311+
// From<Binary> for Vec<u8>
312+
let original = Binary(vec![7u8, 35, 49, 101, 0, 255]);
313+
let original_ptr = original.0.as_ptr();
314+
let vec = Vec::<u8>::from(original);
315+
assert_eq!(vec.as_slice(), [7u8, 35, 49, 101, 0, 255]);
316+
assert_eq!(vec.as_ptr(), original_ptr, "vector must not be copied");
286317
}
287318

288319
#[test]

0 commit comments

Comments
 (0)