Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: borsh and json schemas support #43

Merged
merged 11 commits into from
Jul 23, 2024

Conversation

dzmitry-lahoda
Copy link
Contributor

No description provided.

@dzmitry-lahoda dzmitry-lahoda marked this pull request as ready for review July 2, 2024 17:08
@dzmitry-lahoda dzmitry-lahoda changed the title #wip #draft feat: borsh serde feat: borsh and json schemas support Jul 2, 2024
@dzmitry-lahoda
Copy link
Contributor Author

ping @danlehmann.

@danlehmann
Copy link
Owner

Thanks for the contribution!

I'll work on reviewing this (have to learn about borsh first). Will try to get back asap

src/lib.rs Outdated Show resolved Hide resolved
@dzmitry-lahoda
Copy link
Contributor Author

So what I saw from impl. It is hard to get le bytes of number like u35. Really I can borsh it into 5 bytes. While doing it for 8 now. So support getting LE bytes minimally needed would be awesome, not underlying.

tests/tests.rs Show resolved Hide resolved
tests/tests.rs Outdated Show resolved Hide resolved
tests/tests.rs Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated
[dependencies]
num-traits = { version = "0.2.17", default-features = false, optional = true }
defmt = { version = "0.3.5", optional = true }
serde = { version = "1.0", optional = true, default-features = false}
borsh = { version = "1.5.1", optional = true, features = ["unstable__schema"], default-features = false}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see a way to do this without relying on an unstable feature? I could see this break in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see this break in the future

I can add it as separate feature for BorshSchema?

Problem with borsh is next. Creators of Borsh already use this feature in production. And we too. BorshSchema does not requires nightly, it is just state of thing.

Why I do and have to add it? Because BorshShema is supported for u8/u16/u32/.../i8/... . So in order to replace any number with arbitrary, it to be supported. So what I say is that in our and NEAR chain scenarios, in order to replace Rust native numbers with arbitrary we need Schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLDR;

Assuming everyday will want to plug arbitrary int instead of existing int with highest possible fidelity,
it means that if serde/schema/etc crate does something to native primitive int,
than it must do same for arbitrary-int.

Arbitrary int should blend into like if they are native primitives.

Cargo.toml Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
.github/workflows/test-serde.yml Outdated Show resolved Hide resolved
let mut bytes = 0;
let mask: T = u8::MAX.into();
while bytes < length {
let le_byte: u8 = ((value >> (bytes << 3)) & mask)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified:

let le_bytes = (value >> (bytes << 3)) as u8;

No need for the expect, try_into() etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is not primitive, so 'as' does not work as i tried

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use https://docs.rs/num/latest/num/traits/trait.AsPrimitive.html , but do not want to force num traits usage

tests/tests.rs Outdated Show resolved Hide resolved
tests/tests.rs Show resolved Hide resolved
assert_eq!(buf.len(), (u63::BITS + 7) / 8);
assert_eq!(input, output);

let schema = BorshSchemaContainer::for_type::<u9>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to understand this: BorschSchemaContainer does NOT require schemars? So we have two different things that are called schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One schema is JSON/OpenAPI shema integrated with serde.

Other shema is borshschema integrated with borsh.

There are schemas for protobuf/scale/etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not require, just supporting several schemas

@dzmitry-lahoda
Copy link
Contributor Author

@danlehmann about tests. I tested that arbitrary ints work same as native primitive ints, minus some well know diff.
hence I tested layout, but in the way that I did not plug exact binary values.
this approach allows to extend these tests to property tests and doing meta tests over all serdes/schemas option.

@danlehmann
Copy link
Owner

I'd like to have full binary tests for these. Right now, there are many incorrect implementations that would pass those tests.

However, we can merge and I'll work on the test in a follow-up. Please give me a day or two

Copy link
Owner

@danlehmann danlehmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK let's merge this. Will work on tests in a follow-up

@danlehmann danlehmann merged commit c99730e into danlehmann:main Jul 23, 2024
@danlehmann
Copy link
Owner

This patch didn't actually build (see https://github.com/danlehmann/arbitrary-int/actions/runs/10065181794/job/27823714420)

Working on a fix now

@danlehmann
Copy link
Owner

Reworked things somewhat: #45

@danlehmann
Copy link
Owner

@dzmitry-lahoda Please take a look at #45 . That adds a lot of test coverage, removes the allocation in deserialize and simplifies serialize.

Review appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants