Skip to content

Commit b416d11

Browse files
willco-1dj8yf0μl
andauthored
feat: ser/de for IpAddr type; schema for Ipv4Addr/Ipv6Addr/IpAddr types (#309)
* WIP: add serialization and deserialization for IpAddr type * WIP: add IpAddr to fuzzer * add a test * WIP: add IpAddr to schema * fix: serializer refers to existing ones * fixes * test: roundtrip test for IpAddr * chore: remove assertion * ci: fmt * chore: revert adding implementation for Schema * feat: add derived schema implementation for ipaddr types * ci: remove pkg downgrade section * test: de-macro added rountrip test --------- Co-authored-by: dj8yf0μl <[email protected]>
1 parent f16cd07 commit b416d11

14 files changed

+366
-14
lines changed

.github/workflows/rust.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ jobs:
3636
run: rustup default ${{ matrix.rust_version }}
3737
- name: print rustc version
3838
run: rustc --version
39-
- name: downgrade `toml_edit`, time`, `toml_datetime` crate to support older Rust toolchain
40-
if: matrix.rust_version == '1.67.0'
41-
run: |
42-
cargo update -p toml_edit --precise 0.21.0
39+
# - name: downgrade `toml_edit`, time`, `toml_datetime` crate to support older Rust toolchain
40+
# if: matrix.rust_version == '1.67.0'
41+
# run: |
42+
# cargo update -p toml_edit --precise 0.21.0
4343
- name: Run tests
4444
run: ./.github/test.sh
4545

borsh-derive/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Attribute takes literal string value, which is the syn's [Path] to `borsh` crate
7474
Attribute is optional.
7575
7676
1. If the attribute is not provided, [crate_name](proc_macro_crate::crate_name) is used to find a version of `borsh`
77-
in `[dependencies]` of the relevant `Cargo.toml`. If there is no match, a compilation error, similar to the following, is raised:
77+
in `[dependencies]` of the relevant `Cargo.toml`. If there is no match, a compilation error, similar to the following, is raised:
7878
7979
```bash
8080
1 error: proc-macro derive panicked
@@ -361,7 +361,7 @@ Attribute takes literal string value, which is the syn's [Path] to `borsh` crate
361361
Attribute is optional.
362362
363363
1. If the attribute is not provided, [crate_name](proc_macro_crate::crate_name) is used to find a version of `borsh`
364-
in `[dependencies]` of the relevant `Cargo.toml`. If there is no match, a compilation error, similar to the following, is raised:
364+
in `[dependencies]` of the relevant `Cargo.toml`. If there is no match, a compilation error, similar to the following, is raised:
365365
366366
```bash
367367
1 error: proc-macro derive panicked
@@ -697,7 +697,7 @@ Attribute takes literal string value, which is the syn's [Path] to `borsh` crate
697697
Attribute is optional.
698698
699699
1. If the attribute is not provided, [crate_name](proc_macro_crate::crate_name) is used to find a version of `borsh`
700-
in `[dependencies]` of the relevant `Cargo.toml`. If there is no match, a compilation error, similar to the following, is raised:
700+
in `[dependencies]` of the relevant `Cargo.toml`. If there is no match, a compilation error, similar to the following, is raised:
701701
702702
```bash
703703
1 error: proc-macro derive panicked
@@ -824,7 +824,7 @@ Attribute takes literal string value, which is a comma-separated list of `Parame
824824
It may be used in order to:
825825
826826
1. fix complex cases, when derive hasn't figured out the right bounds on type parameters and
827-
declaration parameters automatically.
827+
declaration parameters automatically.
828828
2. remove parameters, which do not take part in serialization/deserialization, from bounded ones and from declaration parameters.
829829
830830
`ParameterOverride` describes an entry like `order_param => override_type`,

borsh/src/de/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,30 @@ impl BorshDeserialize for std::net::SocketAddr {
659659
}
660660
}
661661

662+
#[cfg(feature = "std")]
663+
impl BorshDeserialize for std::net::IpAddr {
664+
#[inline]
665+
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
666+
let kind = u8::deserialize_reader(reader)?;
667+
match kind {
668+
0u8 => {
669+
// Deserialize an Ipv4Addr and convert it to IpAddr::V4
670+
let ipv4_addr = std::net::Ipv4Addr::deserialize_reader(reader)?;
671+
Ok(std::net::IpAddr::V4(ipv4_addr))
672+
}
673+
1u8 => {
674+
// Deserialize an Ipv6Addr and convert it to IpAddr::V6
675+
let ipv6_addr = std::net::Ipv6Addr::deserialize_reader(reader)?;
676+
Ok(std::net::IpAddr::V6(ipv6_addr))
677+
}
678+
value => Err(Error::new(
679+
ErrorKind::InvalidData,
680+
format!("Invalid IpAddr variant: {}", value),
681+
)),
682+
}
683+
}
684+
}
685+
662686
#[cfg(feature = "std")]
663687
impl BorshDeserialize for std::net::SocketAddrV4 {
664688
#[inline]

borsh/src/schema.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,62 @@ impl_tuple!(
844844
impl_tuple!(
845845
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20
846846
);
847+
848+
#[cfg(feature = "std")]
849+
mod id_addr_std_derive_impl {
850+
use crate::BorshSchema as BorshSchemaMacro;
851+
852+
#[derive(BorshSchemaMacro)]
853+
#[borsh(crate = "crate")]
854+
pub struct Ipv4Addr {
855+
octets: [u8; 4],
856+
}
857+
858+
#[derive(BorshSchemaMacro)]
859+
#[borsh(crate = "crate")]
860+
pub struct Ipv6Addr {
861+
octets: [u8; 16],
862+
}
863+
864+
#[derive(BorshSchemaMacro)]
865+
#[borsh(crate = "crate")]
866+
pub enum IpAddr {
867+
/// An IPv4 address.
868+
V4(std::net::Ipv4Addr),
869+
/// An IPv6 address.
870+
V6(std::net::Ipv6Addr),
871+
}
872+
}
873+
874+
#[cfg(feature = "std")]
875+
impl BorshSchema for std::net::Ipv4Addr {
876+
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
877+
<id_addr_std_derive_impl::Ipv4Addr>::add_definitions_recursively(definitions);
878+
}
879+
880+
fn declaration() -> Declaration {
881+
id_addr_std_derive_impl::Ipv4Addr::declaration()
882+
}
883+
}
884+
885+
#[cfg(feature = "std")]
886+
impl BorshSchema for std::net::Ipv6Addr {
887+
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
888+
<id_addr_std_derive_impl::Ipv6Addr>::add_definitions_recursively(definitions);
889+
}
890+
891+
fn declaration() -> Declaration {
892+
id_addr_std_derive_impl::Ipv6Addr::declaration()
893+
}
894+
}
895+
896+
#[cfg(feature = "std")]
897+
impl BorshSchema for std::net::IpAddr {
898+
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
899+
<id_addr_std_derive_impl::IpAddr>::add_definitions_recursively(definitions);
900+
}
901+
902+
fn declaration() -> Declaration {
903+
id_addr_std_derive_impl::IpAddr::declaration()
904+
}
905+
}

borsh/src/ser/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,22 @@ impl BorshSerialize for std::net::Ipv6Addr {
505505
}
506506
}
507507

508+
#[cfg(feature = "std")]
509+
impl BorshSerialize for std::net::IpAddr {
510+
#[inline]
511+
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
512+
match self {
513+
std::net::IpAddr::V4(ipv4) => {
514+
writer.write_all(&0u8.to_le_bytes())?;
515+
ipv4.serialize(writer)
516+
}
517+
std::net::IpAddr::V6(ipv6) => {
518+
writer.write_all(&1u8.to_le_bytes())?;
519+
ipv6.serialize(writer)
520+
}
521+
}
522+
}
523+
}
508524
impl<T: BorshSerialize + ?Sized> BorshSerialize for Box<T> {
509525
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
510526
self.as_ref().serialize(writer)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
source: borsh/tests/roundtrip/test_ip_addr.rs
3+
expression: encoded
4+
---
5+
[
6+
3,
7+
0,
8+
0,
9+
0,
10+
0,
11+
192,
12+
168,
13+
0,
14+
1,
15+
1,
16+
32,
17+
1,
18+
13,
19+
184,
20+
0,
21+
0,
22+
0,
23+
0,
24+
0,
25+
0,
26+
0,
27+
0,
28+
0,
29+
0,
30+
0,
31+
1,
32+
1,
33+
254,
34+
128,
35+
0,
36+
0,
37+
0,
38+
0,
39+
0,
40+
0,
41+
0,
42+
0,
43+
0,
44+
0,
45+
0,
46+
0,
47+
0,
48+
1,
49+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: borsh/tests/roundtrip/test_ip_addr.rs
3+
expression: encoded
4+
---
5+
[
6+
192,
7+
168,
8+
0,
9+
1,
10+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: borsh/tests/roundtrip/test_ip_addr.rs
3+
expression: encoded
4+
---
5+
[
6+
0,
7+
192,
8+
168,
9+
0,
10+
1,
11+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: borsh/tests/roundtrip/test_ip_addr.rs
3+
expression: encoded
4+
---
5+
[
6+
32,
7+
1,
8+
13,
9+
184,
10+
0,
11+
0,
12+
0,
13+
0,
14+
0,
15+
0,
16+
0,
17+
0,
18+
0,
19+
0,
20+
0,
21+
1,
22+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: borsh/tests/roundtrip/test_ip_addr.rs
3+
expression: encoded
4+
---
5+
[
6+
1,
7+
32,
8+
1,
9+
13,
10+
184,
11+
0,
12+
0,
13+
0,
14+
0,
15+
0,
16+
0,
17+
0,
18+
0,
19+
0,
20+
0,
21+
0,
22+
1,
23+
]

0 commit comments

Comments
 (0)