Skip to content

Commit 7c6061a

Browse files
authored
Feat: type to idl impls for collections (#177)
1 parent ba8832d commit 7c6061a

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

star_frame/src/idl/ty.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! [`TypeToIdl`] implementations for common types.
2+
//!
3+
//! For zero-copy types that are supported by [`bytemuck`], we use that representation. For all other types, we use the [`borsh`] representation.
14
use crate::data_types::PodBool;
25
use crate::idl::TypeToIdl;
36
use crate::program::system::System;
@@ -6,6 +9,7 @@ use solana_pubkey::Pubkey;
69
use star_frame_idl::ty::IdlStructField;
710
use star_frame_idl::ty::IdlTypeDef;
811
use star_frame_idl::IdlDefinition;
12+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
913

1014
macro_rules! impl_type_to_idl_for_primitive {
1115
(@impl $ty:ty: $ident:ident) => {
@@ -68,11 +72,25 @@ impl<T: TypeToIdl> TypeToIdl for Vec<T> {
6872
// supports `Vec<T>` is Borsh, which uses u32 for length.
6973
// Any other serialization would have to be implemented by the user using something
7074
// like the NewType pattern.
71-
len_ty: Box::new(u32::type_to_idl(idl_definition)?),
75+
len_ty: Box::new(IdlTypeDef::U32),
7276
})
7377
}
7478
}
7579

80+
impl<T: TypeToIdl> TypeToIdl for VecDeque<T> {
81+
type AssociatedProgram = System;
82+
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
83+
<Vec<T>>::type_to_idl(idl_definition)
84+
}
85+
}
86+
87+
impl<T: TypeToIdl> TypeToIdl for LinkedList<T> {
88+
type AssociatedProgram = System;
89+
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
90+
<Vec<T>>::type_to_idl(idl_definition)
91+
}
92+
}
93+
7694
impl<T: TypeToIdl, const N: usize> TypeToIdl for [T; N] {
7795
type AssociatedProgram = System;
7896
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
@@ -83,6 +101,41 @@ impl<T: TypeToIdl, const N: usize> TypeToIdl for [T; N] {
83101
}
84102
}
85103

104+
impl<T: TypeToIdl> TypeToIdl for BTreeSet<T> {
105+
type AssociatedProgram = System;
106+
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
107+
Ok(IdlTypeDef::Set {
108+
item_ty: Box::new(T::type_to_idl(idl_definition)?),
109+
len_ty: Box::new(IdlTypeDef::U32),
110+
})
111+
}
112+
}
113+
114+
impl<K: TypeToIdl, V: TypeToIdl> TypeToIdl for BTreeMap<K, V> {
115+
type AssociatedProgram = System;
116+
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
117+
Ok(IdlTypeDef::Map {
118+
key_ty: Box::new(K::type_to_idl(idl_definition)?),
119+
value_ty: Box::new(V::type_to_idl(idl_definition)?),
120+
len_ty: Box::new(IdlTypeDef::U32),
121+
})
122+
}
123+
}
124+
125+
impl<T: TypeToIdl, S> TypeToIdl for HashSet<T, S> {
126+
type AssociatedProgram = System;
127+
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
128+
<BTreeSet<T>>::type_to_idl(idl_definition)
129+
}
130+
}
131+
132+
impl<K: TypeToIdl, V: TypeToIdl, S> TypeToIdl for HashMap<K, V, S> {
133+
type AssociatedProgram = System;
134+
fn type_to_idl(idl_definition: &mut IdlDefinition) -> Result<IdlTypeDef> {
135+
<BTreeMap<K, V>>::type_to_idl(idl_definition)
136+
}
137+
}
138+
86139
macro_rules! impl_type_to_idl_for_tuple {
87140
($($ty:ident),*) => {
88141
impl<$($ty: TypeToIdl),*> TypeToIdl for ($($ty,)*) {

0 commit comments

Comments
 (0)