Skip to content

Commit 8a518f1

Browse files
author
Thaumy
authored
Merge pull request #35 from saying121/bindgen
feat(rust): impl useful trait
2 parents 3fed3f3 + 7aff49c commit 8a518f1

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build
1+
name: Check proto
22

33
on:
44
push:
@@ -36,3 +36,7 @@ jobs:
3636
run: |
3737
if [ ! -d target ]; then mkdir target; fi
3838
protoc --rust_opt='experimental-codegen=enabled,kernel=cpp' --rust_out=./target psh.proto
39+
40+
- name: Test
41+
run: |
42+
cargo test --workspace

rust/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
tonic::include_proto!("psh.proto.instance");
2+
3+
impl From<std::net::Ipv6Addr> for Ipv6Addr {
4+
fn from(value: std::net::Ipv6Addr) -> Self {
5+
let ip = value.to_bits().to_be();
6+
let high = (ip >> 64) as u64;
7+
let low = ip as u64;
8+
Self {
9+
hi_64_bits: high,
10+
lo_64_bits: low,
11+
}
12+
}
13+
}
14+
15+
#[test]
16+
fn test_ipv6_into_pb_repr() {
17+
use std::net::Ipv6Addr as StdIpv6Addr;
18+
19+
let var: u128 = 1;
20+
21+
let raw = StdIpv6Addr::from_bits(var);
22+
23+
let pb_repr: Ipv6Addr = raw.into();
24+
25+
let hi = (pb_repr.hi_64_bits as u128) << 64;
26+
let lo = pb_repr.lo_64_bits as u128;
27+
let ip = StdIpv6Addr::from_bits(u128::from_be(hi | lo));
28+
29+
assert_eq!(ip, StdIpv6Addr::from_bits(1));
30+
}

0 commit comments

Comments
 (0)