Struct Data

Source
pub struct Data(/* private fields */);
Expand description

A variable-size byte array wrapper for safely handling binary data of arbitrary length.

Data provides a flexible container for binary data that doesn’t have a fixed size, such as:

  • Encrypted memo fields
  • Variable-length signatures
  • Script data
  • Other protocol elements that can vary in size

Unlike Blob<N>, which is for fixed-size data, Data can hold any amount of bytes and can grow or shrink as needed.

§Zcash Concept Relation

Zcash uses variable-length data structures in many parts of the protocol:

  • Encrypted memos in shielded transactions
  • Script data in transparent transactions
  • Signatures and other cryptographic proofs

§Data Preservation

Data preserves variable-length binary blobs exactly as they appear in wallet files, ensuring that encrypted data, scripts, and other variable-length content maintain their exact representation during the migration process.

§Examples

// Create from raw bytes
let bytes = vec![1, 2, 3, 4, 5];
let data = Data::from_vec(bytes.clone());
assert_eq!(data.len(), 5);

// Access via indexing
assert_eq!(data[0], 1);
assert_eq!(&data[1..3], &[2, 3]);

Implementations§

Source§

impl Data

Source

pub fn new() -> Self

Creates a new empty Data instance.

§Examples
let data = Data::new();
assert!(data.is_empty());
Source

pub fn from_bytes(data: impl AsRef<[u8]>) -> Self

Creates a Data instance from anything that can be referenced as a byte slice.

This is a convenience method that allows creating a Data from various byte-containing types.

§Examples
// From a slice
let data1 = Data::from_bytes(&[1, 2, 3]);

// From a Vec
let data2 = Data::from_bytes(vec![1, 2, 3]);

// From another Data instance
let data3 = Data::from_bytes(&data1);

assert_eq!(data1, data2);
assert_eq!(data2, data3);
Source

pub fn len(&self) -> usize

Returns the number of bytes in the data.

§Examples
let data = Data::from_bytes(&[1, 2, 3]);
assert_eq!(data.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the data contains no bytes.

§Examples
let empty = Data::new();
assert!(empty.is_empty());

let non_empty = Data::from_bytes(&[1, 2, 3]);
assert!(!non_empty.is_empty());
Source

pub fn to_vec(&self) -> Vec<u8>

Converts the data to a Vec<u8>, creating a copy.

§Examples
let data = Data::from_bytes(&[1, 2, 3]);
let vec = data.to_vec();
assert_eq!(vec, vec![1, 2, 3]);
Source

pub fn from_slice(data: &[u8]) -> Self

Creates a Data instance from a byte slice.

§Examples
let slice = &[1, 2, 3];
let data = Data::from_slice(slice);
assert_eq!(data.to_vec(), slice);
Source

pub fn from_vec(data: Vec<u8>) -> Self

Creates a Data instance from a Vec<u8>, taking ownership of the vector.

This is more efficient than from_slice when you already have a vector, as it avoids making a copy of the data.

§Examples
let vec = vec![1, 2, 3];
let data = Data::from_vec(vec.clone());
assert_eq!(data.to_vec(), vec);
Source

pub fn from_hex(hex: &str) -> Result<Self>

Creates a Data instance from a hexadecimal string.

§Errors

Returns an error if the hex string is invalid (contains non-hex characters or has an odd length).

§Examples
let hex = "010203";
let data = Data::from_hex(hex).unwrap();
assert_eq!(data.to_vec(), vec![1, 2, 3]);

// Invalid hex string
let result = Data::from_hex("01020Z");
assert!(result.is_err());
Source

pub fn concat(a: &[&dyn AsRef<[u8]>]) -> Self

Concatenates multiple byte arrays into a single Data instance.

This is useful for combining multiple binary data sources into one.

§Examples

let data1: &[u8] = &[1, 2];
let data2: &[u8] = &[3, 4];
let combined = Data::concat(&[&data1, &data2]);

assert_eq!(combined.to_vec(), vec![1, 2, 3, 4]);

Trait Implementations§

Source§

impl AsRef<[u8]> for Data

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Data> for Data

Source§

fn as_ref(&self) -> &Data

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Data

Source§

fn clone(&self) -> Data

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Data

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Data

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&[u8]> for Data

Source§

fn from(data: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&Data> for CBOR

Source§

fn from(data: &Data) -> Self

Converts to this type from the input type.
Source§

impl From<&Data> for Vec<u8>

Source§

fn from(data: &Data) -> Vec<u8>

Converts to this type from the input type.
Source§

impl From<Data> for CBOR

Source§

fn from(data: Data) -> Self

Converts to this type from the input type.
Source§

impl From<Data> for Envelope

Source§

fn from(value: Data) -> Self

Converts to this type from the input type.
Source§

impl From<Data> for Script

Creates a Script from Data, allowing conversion from variable-length bytes

Source§

fn from(data: Data) -> Self

Converts to this type from the input type.
Source§

impl From<Data> for Vec<u8>

Source§

fn from(data: Data) -> Vec<u8>

Converts to this type from the input type.
Source§

impl From<Script> for Data

Converts a Script to a Data value, allowing manipulation as variable-length bytes

Source§

fn from(script: Script) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Data

Source§

fn from(data: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl Hash for Data

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<Range<usize>> for Data

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, range: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFrom<usize>> for Data

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, range: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFull> for Data

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, range: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeInclusive<usize>> for Data

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, range: RangeInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeTo<usize>> for Data

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, range: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeToInclusive<usize>> for Data

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, range: RangeToInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for Data

Source§

type Output = u8

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Range<usize>> for Data

Source§

fn index_mut(&mut self, range: Range<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeFrom<usize>> for Data

Source§

fn index_mut(&mut self, range: RangeFrom<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeFull> for Data

Source§

fn index_mut(&mut self, range: RangeFull) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeInclusive<usize>> for Data

Source§

fn index_mut(&mut self, range: RangeInclusive<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeTo<usize>> for Data

Source§

fn index_mut(&mut self, range: RangeTo<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<RangeToInclusive<usize>> for Data

Source§

fn index_mut(&mut self, range: RangeToInclusive<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Data

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Ord for Data

Source§

fn cmp(&self, other: &Data) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Data

Source§

fn eq(&self, other: &Data) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Data

Source§

fn partial_cmp(&self, other: &Data) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<CBOR> for Data

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(cbor: CBOR) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<Envelope> for Data

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(envelope: Envelope) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for Data

Source§

impl StructuralPartialEq for Data

Auto Trait Implementations§

§

impl Freeze for Data

§

impl RefUnwindSafe for Data

§

impl Send for Data

§

impl Sync for Data

§

impl Unpin for Data

§

impl UnwindSafe for Data

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> EnvelopeEncodable for T
where T: Into<Envelope> + Clone,

Source§

fn into_envelope(self) -> Envelope

Converts the value into an envelope by using its Into<Envelope> implementation.

Source§

fn to_envelope(&self) -> Envelope
where Self: Clone,

Converts a reference to this value into a Gordian Envelope. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T