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
impl Data
Sourcepub fn from_bytes(data: impl AsRef<[u8]>) -> Self
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);
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn to_vec(&self) -> Vec<u8> ⓘ
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]);
Sourcepub fn from_slice(data: &[u8]) -> Self
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);
Sourcepub fn from_vec(data: Vec<u8>) -> Self
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);
Sourcepub fn from_hex(hex: &str) -> Result<Self>
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());
Sourcepub fn concat(a: &[&dyn AsRef<[u8]>]) -> Self
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 From<Data> for Script
Creates a Script from Data, allowing conversion from variable-length bytes
impl From<Data> for Script
Creates a Script from Data, allowing conversion from variable-length bytes
Source§impl From<Script> for Data
Converts a Script to a Data value, allowing manipulation as variable-length bytes
impl From<Script> for Data
Converts a Script to a Data value, allowing manipulation as variable-length bytes
Source§impl Ord for Data
impl Ord for Data
Source§impl PartialOrd for Data
impl PartialOrd for Data
impl Eq for Data
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> EnvelopeEncodable for T
impl<T> EnvelopeEncodable for T
Source§fn into_envelope(self) -> Envelope
fn into_envelope(self) -> Envelope
Converts the value into an envelope by using its Into<Envelope>
implementation.
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Lower case
letters are used (e.g. f9b4ca
)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Upper case
letters are used (e.g. F9B4CA
)