Skip to content

Commit

Permalink
chore!: make BorshSchema::add_definition default implementation a f…
Browse files Browse the repository at this point in the history
…ree-standing func (#204)
  • Loading branch information
dj8yfo authored Aug 29, 2023
1 parent c1a72c4 commit 336daa7
Show file tree
Hide file tree
Showing 42 changed files with 90 additions and 87 deletions.
2 changes: 1 addition & 1 deletion borsh-derive/src/internals/schema/enums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn process(input: &ItemEnum, cratename: Ident) -> syn::Result<TokenStream2>
#add_recursive_defs
let variants = #cratename::__private::maybestd::vec![#(#variants_defs),*];
let definition = #cratename::schema::Definition::Enum{variants};
Self::add_definition(Self::declaration(), definition, definitions);
#cratename::schema::add_definition(Self::declaration(), definition, definitions);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
let definition = borsh::schema::Definition::Enum {
variants,
};
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
}
}

2 changes: 1 addition & 1 deletion borsh-derive/src/internals/schema/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn process(input: &ItemStruct, cratename: Ident) -> syn::Result<TokenStream2
let definition = #cratename::schema::Definition::Struct { fields };

let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
#cratename::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
#add_definitions_recursively
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<T::Associated as borsh::BorshSchema>::add_definitions_recursively(
definitions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<<T as TraitName>::Associated as borsh::BorshSchema>::add_definitions_recursively(
definitions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<(
<T as TraitName>::Associated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<U as borsh::BorshSchema>::add_definitions_recursively(definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<U as borsh::BorshSchema>::add_definitions_recursively(definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<HashMap<
K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<U as borsh::BorshSchema>::add_definitions_recursively(definitions);
<K as borsh::BorshSchema>::add_definitions_recursively(definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<C> borsh::BorshSchema for ASalad<C> {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<Tomatoes as borsh::BorshSchema>::add_definitions_recursively(definitions);
<Oil as borsh::BorshSchema>::add_definitions_recursively(definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl borsh::BorshSchema for CRecC {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<String as borsh::BorshSchema>::add_definitions_recursively(definitions);
<HashMap<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<PrimaryMap<
K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<HashMap<
K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl borsh::BorshSchema for A {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<u64 as borsh::BorshSchema>::add_definitions_recursively(definitions);
<String as borsh::BorshSchema>::add_definitions_recursively(definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<HashMap<
K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl borsh::BorshSchema for A {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<u64 as borsh::BorshSchema>::add_definitions_recursively(definitions);
<String as borsh::BorshSchema>::add_definitions_recursively(definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<K as borsh::BorshSchema>::add_definitions_recursively(definitions);
<V as borsh::BorshSchema>::add_definitions_recursively(definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl borsh::BorshSchema for A {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<String as borsh::BorshSchema>::add_definitions_recursively(definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl borsh::BorshSchema for A {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl borsh::BorshSchema for A {
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
third_party_impl::add_definitions_recursively::<K, V>(definitions);
<u64 as borsh::BorshSchema>::add_definitions_recursively(definitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ where
fields,
};
let no_recursion_flag = definitions.get(&Self::declaration()).is_none();
Self::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(Self::declaration(), definition, definitions);
if no_recursion_flag {
<T as borsh::BorshSchema>::add_definitions_recursively(definitions);
}
Expand Down
6 changes: 3 additions & 3 deletions borsh/src/generate_schema_schema.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Generate `BorshSchemaCointainer` for `BorshSchemaContainer` and save it into a file.

#![cfg_attr(not(feature = "std"), no_std)]
use borsh::schema::BorshSchema;
use borsh::BorshSerialize;
use borsh::{schema_container_of, BorshSerialize};
use std::fs::File;
use std::io::Write;

fn main() {
let container = borsh::schema::BorshSchemaContainer::schema_container();
let container = schema_container_of::<borsh::schema::BorshSchemaContainer>();

println!("{:#?}", container);
let data = container
.try_to_vec()
Expand Down
6 changes: 3 additions & 3 deletions borsh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
[BorshDeserialize](crate::de::BorshDeserialize) traits.
* **schema** -
Gates [BorshSchema](crate::schema::BorshSchema) trait and its derive macro.
Gates [schema](crate::schema) and [schema_helpers](crate::schema_helpers) modules.
Gates [schema](crate::schema) module.
This feature requires **derive** to be enabled too.
* **rc** -
Gates implementation of [BorshSerialize](crate::ser::BorshSerialize) and [BorshDeserialize](crate::de::BorshDeserialize)
Expand Down Expand Up @@ -77,15 +77,15 @@ pub mod de;
pub mod schema;
/// Module is available if borsh is built with `features = ["derive", "schema"]`.
#[cfg(derive_schema)]
pub mod schema_helpers;
pub(crate) mod schema_helpers;
pub mod ser;

pub use de::BorshDeserialize;
pub use de::{from_reader, from_slice};
#[cfg(derive_schema)]
pub use schema::BorshSchema;
#[cfg(derive_schema)]
pub use schema_helpers::{try_from_slice_with_schema, try_to_vec_with_schema};
pub use schema_helpers::{schema_container_of, try_from_slice_with_schema, try_to_vec_with_schema};
pub use ser::helpers::{to_vec, to_writer};
pub use ser::BorshSerialize;
pub mod error;
Expand Down
Loading

0 comments on commit 336daa7

Please sign in to comment.