-
Notifications
You must be signed in to change notification settings - Fork 249
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(json_types): implement FromStr for Base58CryptoHash and cleanup #398
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29204d6
feat: implement FromStr for Base58CryptoHash and cleanup
austinabell 0051773
chore: cleanup import
austinabell fdeaef7
chore: create alias for error type to easily switch in future
austinabell fb9487f
chore: cleanup namespacing
austinabell d154871
fix: fmt
austinabell 96fbc2d
Merge branch 'master' into b58hash_fstr
austinabell 7dbc66d
switch new impl to concrete type
austinabell db3d48b
Merge branch 'master' of github.com:near/near-sdk-rs into b58hash_fstr
austinabell 65633aa
Merge branch 'b58hash_fstr' of github.com:austinabell/near-sdk-rs int…
austinabell a38294f
Merge branch 'master' into b58hash_fstr
mikedotexe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
use crate::CryptoHash; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use std::convert::{TryFrom, TryInto}; | ||
use serde::{de, ser, Deserialize}; | ||
use std::borrow::Cow; | ||
use std::convert::TryFrom; | ||
|
||
type Error = Box<dyn std::error::Error>; | ||
|
||
#[derive( | ||
Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, BorshDeserialize, BorshSerialize, Default, | ||
|
@@ -19,26 +23,22 @@ impl From<CryptoHash> for Base58CryptoHash { | |
} | ||
} | ||
|
||
impl serde::Serialize for Base58CryptoHash { | ||
fn serialize<S>( | ||
&self, | ||
serializer: S, | ||
) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error> | ||
impl ser::Serialize for Base58CryptoHash { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
S: ser::Serializer, | ||
{ | ||
serializer.serialize_str(&String::from(self)) | ||
} | ||
} | ||
|
||
impl<'de> serde::Deserialize<'de> for Base58CryptoHash { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, <D as serde::Deserializer<'de>>::Error> | ||
impl<'de> de::Deserialize<'de> for Base58CryptoHash { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
D: de::Deserializer<'de>, | ||
{ | ||
let s = <String as serde::Deserialize>::deserialize(deserializer)?; | ||
s.try_into() | ||
.map_err(|err: Box<dyn std::error::Error>| serde::de::Error::custom(err.to_string())) | ||
let s: Cow<'de, str> = Deserialize::deserialize(deserializer)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice optimization! |
||
s.parse::<Self>().map_err(|err| de::Error::custom(err.to_string())) | ||
} | ||
} | ||
|
||
|
@@ -49,17 +49,25 @@ impl From<&Base58CryptoHash> for String { | |
} | ||
|
||
impl TryFrom<String> for Base58CryptoHash { | ||
type Error = Box<dyn std::error::Error>; | ||
type Error = Error; | ||
|
||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
Self::try_from(value.as_str()) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for Base58CryptoHash { | ||
type Error = Box<dyn std::error::Error>; | ||
type Error = Error; | ||
|
||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
value.parse() | ||
} | ||
} | ||
|
||
impl std::str::FromStr for Base58CryptoHash { | ||
type Err = Error; | ||
|
||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
let mut crypto_hash: CryptoHash = CryptoHash::default(); | ||
let size = bs58::decode(value).into(&mut crypto_hash)?; | ||
if size != std::mem::size_of::<CryptoHash>() { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should try to rebuild some contract that uses
Base58CryptoHash
with the contract builder, to compare the compilation size with usingCow
. I don't expect anything dramatic, but just to be carefulThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you give me a workflow or point me in the direction of one that does, I can do that. I'd be curious to see if it actually increases the compiled code (and to what scale).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what's the existing workflow, but I'd suggest adding a smoke test for this.
Something stupid like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use Base58CryptoHash in
examples/test-contract
and compile it before the change and after the change using contract builder https://github.com/near/near-sdk-rs/tree/master/contact-builder and compare binary sizes