From c9f95836072d4bb0e0112dc252ef1367e0d8616e Mon Sep 17 00:00:00 2001 From: tbro <48967308+tbro@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:03:34 -0300 Subject: [PATCH] Add `from_bytes` method. (#2329) * Add `from_bytes_unchecked` method. Closes #2327 --------- Co-authored-by: tbro --- types/src/v0/impls/block/full_payload/ns_table.rs | 7 +++++++ types/src/v0/impls/block/full_payload/ns_table/test.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/types/src/v0/impls/block/full_payload/ns_table.rs b/types/src/v0/impls/block/full_payload/ns_table.rs index 994d68710b..bbab22eb85 100644 --- a/types/src/v0/impls/block/full_payload/ns_table.rs +++ b/types/src/v0/impls/block/full_payload/ns_table.rs @@ -100,6 +100,13 @@ impl NsTable { self.len().in_bounds(index) } + /// Instantiate an `NsTable` from a byte slice. + pub fn from_bytes_unchecked(bytes: &[u8]) -> NsTable { + NsTable { + bytes: bytes.to_vec(), + } + } + /// Are the bytes of this [`NsTable`] uncorrupted? /// /// # Checks diff --git a/types/src/v0/impls/block/full_payload/ns_table/test.rs b/types/src/v0/impls/block/full_payload/ns_table/test.rs index 2c515651d7..676c89da44 100644 --- a/types/src/v0/impls/block/full_payload/ns_table/test.rs +++ b/types/src/v0/impls/block/full_payload/ns_table/test.rs @@ -25,6 +25,13 @@ fn random_valid() { } } +#[test] +fn ns_table_from_bytes() { + let bytes = Vec::from([0; NUM_NSS_BYTE_LEN]); + let ns_table = NsTable::from_bytes_unchecked(&bytes); + expect_valid(&ns_table); +} + #[test] fn ns_table_byte_len() { setup_test();