Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions type-length-value/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ derive = ["dep:spl-type-length-value-derive", "solana-program-error/borsh"]
[dependencies]
bytemuck = { version = "1.23.2", features = ["derive"] }
num-derive = "0.4"
num_enum = "0.7"
num-traits = "0.2"
solana-account-info = "3.0.0"
solana-msg = "3.0.0"
num_enum = { version = "0.7", default-features = false }
num-traits = { version = "0.2", default-features = false }
solana-account-info = "3.1.1"
solana-program-error = "3.0.0"
solana-zero-copy = { version = "1.0.0", features = ["bytemuck"] }
spl-discriminator = { version = "0.5.1", path = "../discriminator" }
spl-type-length-value-derive = { version = "0.2", path = "../type-length-value-derive", optional = true }
spl-pod = { version = "0.7.1", path = "../pod", features = ["bytemuck"] }
thiserror = "2.0"
thiserror = { version = "2.0", default-features = false }

[lib]
crate-type = ["lib"]
Expand Down
6 changes: 3 additions & 3 deletions type-length-value/src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
use {
bytemuck::{Pod, Zeroable},
solana_program_error::ProgramError,
spl_pod::primitives::PodU32,
solana_zero_copy::unaligned::U32,
};

/// Length in TLV structure
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
#[repr(transparent)]
pub struct Length(PodU32);
pub struct Length(U32);
impl TryFrom<Length> for usize {
type Error = ProgramError;
fn try_from(n: Length) -> Result<Self, Self::Error> {
Expand All @@ -19,7 +19,7 @@ impl TryFrom<usize> for Length {
type Error = ProgramError;
fn try_from(n: usize) -> Result<Self, Self::Error> {
u32::try_from(n)
.map(|v| Self(PodU32::from(v)))
.map(|v| Self(U32::from(v)))
.map_err(|_| ProgramError::AccountDataTooSmall)
}
}
3 changes: 3 additions & 0 deletions type-length-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

#![allow(clippy::arithmetic_side_effects)]
#![deny(missing_docs)]
#![no_std]
#![cfg_attr(not(test), forbid(unsafe_code))]

extern crate alloc;

pub mod error;
pub mod length;
pub mod state;
Expand Down
17 changes: 13 additions & 4 deletions type-length-value/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

use {
crate::{error::TlvError, length::Length, variable_len_pack::VariableLenPack},
bytemuck::Pod,
alloc::{vec, vec::Vec},
bytemuck::{try_from_bytes, try_from_bytes_mut, Pod},
core::{cmp::Ordering, mem::size_of},
solana_account_info::AccountInfo,
solana_program_error::ProgramError,
spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
spl_pod::bytemuck::{pod_from_bytes, pod_from_bytes_mut},
std::{cmp::Ordering, mem::size_of},
};

fn pod_from_bytes<T: Pod>(bytes: &[u8]) -> Result<&T, ProgramError> {
try_from_bytes(bytes).map_err(|_| ProgramError::InvalidArgument)
}

fn pod_from_bytes_mut<T: Pod>(bytes: &mut [u8]) -> Result<&mut T, ProgramError> {
try_from_bytes_mut(bytes).map_err(|_| ProgramError::InvalidArgument)
}

/// Get the current `TlvIndices` from the current spot
const fn get_indices_unchecked(type_start: usize, value_repetition_number: usize) -> TlvIndices {
let length_start = type_start.saturating_add(size_of::<ArrayDiscriminator>());
Expand Down Expand Up @@ -611,6 +619,7 @@ fn check_data(tlv_data: &[u8]) -> Result<(), ProgramError> {
mod test {
use {
super::*,
alloc::string::{String, ToString},
bytemuck::{Pod, Zeroable},
};

Expand Down Expand Up @@ -1142,7 +1151,7 @@ mod test {
if src[8..8 + length].len() != length {
return Err(ProgramError::InvalidAccountData);
}
let data = std::str::from_utf8(&src[8..8 + length])
let data = core::str::from_utf8(&src[8..8 + length])
.unwrap()
.to_string();
Ok(Self { data })
Expand Down
Loading