Skip to content
Closed
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
16 changes: 16 additions & 0 deletions .changeset/violet-brooms-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@codama/renderers-rust': major
---

Make generated Rust clients `no_std`-compatible by default.

Generated Rust output now emits `extern crate alloc`, replaces `std` collection and string usage with `alloc`-backed equivalents, and fails rendering if any `std::` references remain in generated files.

This changes part of the generated API surface:
- map and set types now use `BTreeMap` and `BTreeSet`
- generated scalar enums now also derive `Ord` by default
- Borsh serialization and deserialization helpers now return `borsh::io::Error` instead of `std::io::Error`

When syncing Cargo dependencies, the renderer now disables default features for crates that would otherwise pull in `std` and upgrades `thiserror` to v2 with `default-features = false`.

This is a breaking change because upgrading changes generated Rust types, error types, and synced Cargo dependency declarations for existing clients.
6 changes: 3 additions & 3 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ resolver = "2"

[workspace.dependencies]
anchor-lang = "~0.31"
borsh = "^1.0"
borsh = { version = "^1.0", default-features = false, features = ["derive"] }
num-derive = "^0.4"
num-traits = "^0.2"
num-traits = { version = "^0.2", default-features = false }
solana-account = "~3.0"
solana-account-info = "~3.1"
solana-address = "~2.2"
solana-client = "~3.0"
solana-cpi = "~3.1"
solana-decode-error = "~2.3"
solana-instruction = "~3.2"
solana-instruction = { version = "~3.2", default-features = false }
solana-program-error = "~3.0"
spl-collections = "0.1.1"
thiserror = "^1.0"
thiserror = { version = "^2.0", default-features = false }
31 changes: 18 additions & 13 deletions e2e/anchor/src/generated/accounts/guard_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use crate::generated::types::CpiRule;
use crate::generated::types::MetadataAdditionalFieldRule;
use crate::generated::types::TransferAmountRule;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_address::Address;
Expand All @@ -31,14 +32,14 @@ pub const GUARD_V1_DISCRIMINATOR: [u8; 8] = [185, 149, 156, 78, 245, 108, 172, 6

impl GuardV1 {
#[inline(always)]
pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
pub fn from_bytes(data: &[u8]) -> Result<Self, borsh::io::Error> {
let mut data = data;
Self::deserialize(&mut data)
}
}

impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for GuardV1 {
type Error = std::io::Error;
type Error = borsh::io::Error;

fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
let mut data: &[u8] = &(*account_info.data).borrow();
Expand All @@ -50,7 +51,7 @@ impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for GuardV1 {
pub fn fetch_guard_v1(
rpc: &solana_client::rpc_client::RpcClient,
address: &solana_address::Address,
) -> Result<crate::shared::DecodedAccount<GuardV1>, std::io::Error> {
) -> Result<crate::shared::DecodedAccount<GuardV1>, borsh::io::Error> {
let accounts = fetch_all_guard_v1(rpc, &[*address])?;
Ok(accounts[0].clone())
}
Expand All @@ -59,16 +60,19 @@ pub fn fetch_guard_v1(
pub fn fetch_all_guard_v1(
rpc: &solana_client::rpc_client::RpcClient,
addresses: &[solana_address::Address],
) -> Result<Vec<crate::shared::DecodedAccount<GuardV1>>, std::io::Error> {
) -> Result<alloc::vec::Vec<crate::shared::DecodedAccount<GuardV1>>, borsh::io::Error> {
let accounts = rpc
.get_multiple_accounts(addresses)
.map_err(|e| std::io::Error::other(e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::DecodedAccount<GuardV1>> = Vec::new();
.map_err(|e| borsh::io::Error::other(alloc::format!("{e}")))?;
let mut decoded_accounts: alloc::vec::Vec<crate::shared::DecodedAccount<GuardV1>> =
alloc::vec::Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
let account = accounts[i].as_ref().ok_or(std::io::Error::other(format!(
"Account not found: {address}"
)))?;
let account = accounts[i]
.as_ref()
.ok_or(borsh::io::Error::other(alloc::format!(
"Account not found: {address}"
)))?;
let data = GuardV1::from_bytes(&account.data)?;
decoded_accounts.push(crate::shared::DecodedAccount {
address,
Expand All @@ -83,7 +87,7 @@ pub fn fetch_all_guard_v1(
pub fn fetch_maybe_guard_v1(
rpc: &solana_client::rpc_client::RpcClient,
address: &solana_address::Address,
) -> Result<crate::shared::MaybeAccount<GuardV1>, std::io::Error> {
) -> Result<crate::shared::MaybeAccount<GuardV1>, borsh::io::Error> {
let accounts = fetch_all_maybe_guard_v1(rpc, &[*address])?;
Ok(accounts[0].clone())
}
Expand All @@ -92,11 +96,12 @@ pub fn fetch_maybe_guard_v1(
pub fn fetch_all_maybe_guard_v1(
rpc: &solana_client::rpc_client::RpcClient,
addresses: &[solana_address::Address],
) -> Result<Vec<crate::shared::MaybeAccount<GuardV1>>, std::io::Error> {
) -> Result<alloc::vec::Vec<crate::shared::MaybeAccount<GuardV1>>, borsh::io::Error> {
let accounts = rpc
.get_multiple_accounts(addresses)
.map_err(|e| std::io::Error::other(e.to_string()))?;
let mut decoded_accounts: Vec<crate::shared::MaybeAccount<GuardV1>> = Vec::new();
.map_err(|e| borsh::io::Error::other(alloc::format!("{e}")))?;
let mut decoded_accounts: alloc::vec::Vec<crate::shared::MaybeAccount<GuardV1>> =
alloc::vec::Vec::new();
for i in 0..addresses.len() {
let address = addresses[i];
if let Some(account) = accounts[i].as_ref() {
Expand Down
7 changes: 5 additions & 2 deletions e2e/anchor/src/generated/instructions/create_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use crate::generated::types::CpiRule;
use crate::generated::types::MetadataAdditionalFieldRule;
use crate::generated::types::TransferAmountRule;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -93,7 +96,7 @@ impl CreateGuardInstructionData {
}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand All @@ -115,7 +118,7 @@ pub struct CreateGuardInstructionArgs {
}

impl CreateGuardInstructionArgs {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
6 changes: 4 additions & 2 deletions e2e/anchor/src/generated/instructions/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -91,7 +93,7 @@ impl ExecuteInstructionData {
}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand All @@ -108,7 +110,7 @@ pub struct ExecuteInstructionArgs {
}

impl ExecuteInstructionArgs {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
4 changes: 3 additions & 1 deletion e2e/anchor/src/generated/instructions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -79,7 +81,7 @@ impl InitializeInstructionData {
}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
6 changes: 4 additions & 2 deletions e2e/anchor/src/generated/instructions/update_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use crate::generated::types::CpiRule;
use crate::generated::types::MetadataAdditionalFieldRule;
use crate::generated::types::TransferAmountRule;
use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -86,7 +88,7 @@ impl UpdateGuardInstructionData {
}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand All @@ -105,7 +107,7 @@ pub struct UpdateGuardInstructionArgs {
}

impl UpdateGuardInstructionArgs {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
2 changes: 2 additions & 0 deletions e2e/anchor/src/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

extern crate alloc;

pub mod accounts;
pub mod errors;
pub mod instructions;
Expand Down
1 change: 1 addition & 0 deletions e2e/anchor/src/generated/types/cpi_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use solana_address::Address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::string::String;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//!

use crate::generated::types::MetadataAdditionalFieldRestriction;
use alloc::string::String;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down
4 changes: 4 additions & 0 deletions e2e/anchor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![no_std]

extern crate alloc;

mod generated;

pub use generated::programs::WEN_TRANSFER_GUARD_ID as ID;
Expand Down
4 changes: 3 additions & 1 deletion e2e/dummy/src/generated/instructions/instruction1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -42,7 +44,7 @@ impl Instruction1InstructionData {
Self {}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
4 changes: 3 additions & 1 deletion e2e/dummy/src/generated/instructions/instruction2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -42,7 +44,7 @@ impl Instruction2InstructionData {
Self {}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
4 changes: 3 additions & 1 deletion e2e/dummy/src/generated/instructions/instruction3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -46,7 +48,7 @@ impl Instruction3InstructionData {
Self { discriminator: 42 }
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
6 changes: 4 additions & 2 deletions e2e/dummy/src/generated/instructions/instruction4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -48,7 +50,7 @@ impl Instruction4InstructionData {
Self {}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand All @@ -65,7 +67,7 @@ pub struct Instruction4InstructionArgs {
}

impl Instruction4InstructionArgs {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
6 changes: 4 additions & 2 deletions e2e/dummy/src/generated/instructions/instruction5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! <https://github.com/codama-idl/codama>
//!

use alloc::boxed::Box;
use alloc::vec::Vec;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

Expand Down Expand Up @@ -48,7 +50,7 @@ impl Instruction5InstructionData {
Self {}
}

pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand All @@ -65,7 +67,7 @@ pub struct Instruction5InstructionArgs {
}

impl Instruction5InstructionArgs {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, borsh::io::Error> {
borsh::to_vec(self)
}
}
Expand Down
Loading
Loading