-3

bytes32 is a type that is a 32-bit-wide byte array in Vyper. The following is from https://github.com/ethereum/vyper/blob/master/docs/types.rst#32-bit-wide-byte-array:


32-bit-wide Byte Array

Keyword: bytes32 This is a 32-bit-wide byte array that is otherwise similiar to byte arrays.

Example:

# Declaration
hash: bytes32
# Assignment
self.hash = _hash

Operators

====================================  ============================================================
Keyword                               Description
====================================  ============================================================
``len(x)``                            Return the length as an integer.
``sha3(x)``                           Return the sha3 hash as bytes32.
``concat(x, ...)``                    Concatenate multiple inputs.
``slice(x, start=_start, len=_len)``  Return a slice of ``_len`` starting at ``_start``.
====================================  ============================================================

Where x is a byte array and _start as well as _len are integer values.


I want to know how to create such a bytes32 as a custom type in Rust. To create custom types you use a struct, and it's an array, but I'm not sure what the best way to define the array is. I thought of doing:

struct Bytes32 {
    bytes32: [0b00000000; 4]
}

But this is obviously not ideal e.g. for readability, and you have to use a specific value, 0b00000000.

James Ray
  • 424
  • 3
  • 15
  • Please, read the [Rust book](https://doc.rust-lang.org/book/second-edition/) before asking trivial questions like this. – Boiethios Mar 26 '18 at 13:26
  • 3
    BTW, the answer is `struct Bytes32([u8; 4]);` – Boiethios Mar 26 '18 at 13:32
  • OK thanks. I have read the Rust book and through parts of the standard library, but I guess I still wasn't sure. I knew that I needed to make a struct to create a custom type, but I wasn't sure what the best way to actually define that type is. But I agree that using an array `[u8; 4]` is simple and effective, it's a 4 byte wide array. – James Ray Mar 26 '18 at 22:16
  • 1
    I'll try to make sure that I ask questions in an appropriate way in future. – James Ray Mar 26 '18 at 22:33
  • 1
    I edited my question. – James Ray Mar 29 '18 at 09:02

1 Answers1

-1

You can use a custom struct.

I made a quick example. You should tune it and make a wrapper for input and output.

#[derive(Eq, PartialEq)]
struct Bytes32 {
    pub store: Vec<[u8; 4]>,
}

impl Ord for Bytes32 {
    fn cmp(&self, other: &Bytes32) -> Ordering {
        for _i in 0..3 {
            if other.store[_i] > self.store[_i] {
                return Ordering::Greater;
            }
            if other.store[_i] < self.store[_i] {
                return Ordering::Less;
            }
        }
        Ordering::Equal
    }
}

impl PartialOrd for Bytes32 {
    fn partial_cmp(&self, other: &Bytes32) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37
  • Thanks. This should also be helpful: https://docs.rs/crate/ethcore-bytes – James Ray Mar 26 '18 at 05:57
  • I'm not sure what the most efficient way to tie these two together would be, however. – James Ray Mar 26 '18 at 06:16
  • 5
    `Vec<[&'a u8; 4]>`? What a weird type. – mcarton Mar 26 '18 at 06:52
  • I honestly don't know how to make a wrapper for I/O. I read a bit of [this](https://doc.rust-lang.org/beta/std/vec/struct.Vec.html) and [this](https://doc.rust-lang.org/beta/std/io/). – James Ray Mar 26 '18 at 07:22
  • Oh I think at least part of what you're referring to is error handling, e.g. a wrapper with `Result`. – James Ray Mar 26 '18 at 07:34
  • I've already read the Rust Programming Language docs so I'm familiar with these: https://manishearth.github.io/blog/2015/05/27/wrapper-types-in-rust-choosing-your-guarantees/. – James Ray Mar 26 '18 at 08:02
  • 2
    Why `&u8` instead of plain `u8` as array element? – CodesInChaos Mar 26 '18 at 14:55
  • This answer is also useful for passing a custom type into a HashMap as per https://doc.rust-lang.org/std/collections/struct.HashMap.html. – James Ray Mar 27 '18 at 03:10