Skip to content

Commit 882789a

Browse files
committed
Reduce dependency on standard library in favor of core-only alternatives
1 parent 206fde8 commit 882789a

File tree

4 files changed

+71
-56
lines changed

4 files changed

+71
-56
lines changed

rust/car_example/src/main.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
21
/// Note that the included codec file and the data file further below
32
/// are generated through SBE's gradle tooling. `./gradlew generateRustCodecs`
4-
include!("car_example_generated_codec.rs");
3+
mod car_example_generated_codec;
4+
use car_example_generated_codec::*;
55

66
use std::fs::File;
77
use std::io::prelude::*;
8-
use std::io::Write;
8+
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Write};
99

1010
pub fn main() {
1111
::std::process::exit(match run_car_example() {
@@ -17,15 +17,15 @@ pub fn main() {
1717
});
1818
}
1919

20-
fn read_sbe_file_generated_from_java_example() -> IoResult<Vec<u8>> {
20+
fn read_sbe_file_generated_from_java_example() -> ::std::io::Result<Vec<u8>> {
2121
// Generated by the generateCarExampleDataFile gradle task.
2222
let mut f = File::open("car_example_data.sbe")?;
2323
let mut buffer = Vec::new();
2424
f.read_to_end(&mut buffer)?;
2525
Ok(buffer)
2626
}
2727

28-
fn run_car_example() -> IoResult<()> {
28+
fn run_car_example() -> ::std::io::Result<()> {
2929
let reference_example_bytes = read_sbe_file_generated_from_java_example()?;
3030
decode_car_and_assert_expected_content(&reference_example_bytes)?;
3131
let bytes_encoded_from_rust = encode_car_from_scratch()?;
@@ -34,7 +34,16 @@ fn run_car_example() -> IoResult<()> {
3434
Ok(())
3535
}
3636

37-
fn decode_car_and_assert_expected_content(buffer: &[u8]) -> IoResult<()> {
37+
impl std::convert::From<CodecErr> for IoError {
38+
fn from(codec_err: CodecErr) -> Self {
39+
IoError::new(IoErrorKind::Other, match codec_err {
40+
CodecErr::NotEnoughBytes => "not enough bytes",
41+
CodecErr::SliceIsLongerThanAllowedBySchema => "slice is longer than allowed by schema",
42+
})
43+
}
44+
}
45+
46+
fn decode_car_and_assert_expected_content(buffer: &[u8]) -> CodecResult<()> {
3847
let (h, dec_fields) = start_decoding_car(&buffer).header()?;
3948
assert_eq!(49u16, h.block_length);
4049
assert_eq!(1u16, h.template_id);
@@ -55,7 +64,6 @@ fn decode_car_and_assert_expected_content(buffer: &[u8]) -> IoResult<()> {
5564
let dec_perf_figures_header = match dec_fuel_figures_header.fuel_figures_individually()? {
5665
Either::Left(mut dec_ff_members) => {
5766
println!("Got some fuel figure members");
58-
let mut decoder_after_group = None;
5967
loop {
6068
let (ff_fields, dec_usage_description) = dec_ff_members.next_fuel_figures_member()?;
6169
let (usage_description, next_step) = dec_usage_description.usage_description()?;
@@ -72,12 +80,10 @@ fn decode_car_and_assert_expected_content(buffer: &[u8]) -> IoResult<()> {
7280
match next_step {
7381
Either::Left(more_members) => dec_ff_members = more_members,
7482
Either::Right(done_with_group) => {
75-
decoder_after_group = Some(done_with_group);
76-
break;
83+
break done_with_group;
7784
}
7885
}
7986
}
80-
decoder_after_group.unwrap()
8187
}
8288
Either::Right(next_decoder) => next_decoder,
8389
};
@@ -89,7 +95,6 @@ fn decode_car_and_assert_expected_content(buffer: &[u8]) -> IoResult<()> {
8995

9096
let dec_manufacturer = match dec_perf_figures_header.performance_figures_individually()? {
9197
Either::Left(mut dec_pf_members) => {
92-
let mut decoder_after_pf_group = None;
9398
println!("Got some performance figure members");
9499
loop {
95100
let (pf_fields, dec_acceleration_header) = dec_pf_members
@@ -105,12 +110,10 @@ fn decode_car_and_assert_expected_content(buffer: &[u8]) -> IoResult<()> {
105110
match next_step {
106111
Either::Left(more_members) => dec_pf_members = more_members,
107112
Either::Right(done_with_group) => {
108-
decoder_after_pf_group = Some(done_with_group);
109-
break;
113+
break done_with_group;
110114
}
111115
}
112116
}
113-
decoder_after_pf_group.unwrap()
114117
}
115118
Either::Right(next_decoder) => next_decoder,
116119
};
@@ -136,7 +139,7 @@ fn decode_car_and_assert_expected_content(buffer: &[u8]) -> IoResult<()> {
136139
Ok(())
137140
}
138141

139-
fn encode_car_from_scratch() -> IoResult<Vec<u8>> {
142+
fn encode_car_from_scratch() -> CodecResult<Vec<u8>> {
140143
let mut buffer = vec![0u8; 256];
141144
let used_pos = {
142145
let enc_header = start_encoding_car(&mut buffer);

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustCodecType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void appendDirectCodeMethods(final Appendable appendable, final String methodNam
3131
final String representationType, final String nextCoderType,
3232
final int numBytes) throws IOException
3333
{
34-
indent(appendable, 1, "pub fn %s(mut self) -> IoResult<(&%s %s, %s)> {\n",
34+
indent(appendable, 1, "pub fn %s(mut self) -> CodecResult<(&%s %s, %s)> {\n",
3535
methodName, DATA_LIFETIME, representationType,
3636
RustGenerator.withLifetime(nextCoderType));
3737
indent(appendable, 2, "let v = self.%s.read_type::<%s>(%s)?;\n",
@@ -67,15 +67,15 @@ void appendDirectCodeMethods(final Appendable appendable, final String methodNam
6767
final int numBytes) throws IOException
6868
{
6969

70-
indent(appendable, 1, "pub fn %s(mut self) -> IoResult<(&%s mut %s, %s)> {\n",
70+
indent(appendable, 1, "pub fn %s(mut self) -> CodecResult<(&%s mut %s, %s)> {\n",
7171
methodName, DATA_LIFETIME, representationType, RustGenerator.withLifetime(nextCoderType));
7272
indent(appendable, 2, "let v = self.%s.writable_overlay::<%s>(%s)?;\n",
7373
RustCodecType.Encoder.scratchProperty(), representationType, numBytes);
7474
indent(appendable, 2, "Ok((v, %s::wrap(self.%s)))\n",
7575
nextCoderType, RustCodecType.Encoder.scratchProperty());
7676
indent(appendable).append("}\n");
7777

78-
indent(appendable).append(String.format("pub fn %s_copy(mut self, t: &%s) -> IoResult<%s> {\n",
78+
indent(appendable).append(String.format("pub fn %s_copy(mut self, t: &%s) -> CodecResult<%s> {\n",
7979
methodName, representationType, RustGenerator.withLifetime(nextCoderType)));
8080
indent(appendable, 2)
8181
.append(format("self.%s.write_type::<%s>(t, %s)?;\n",

0 commit comments

Comments
 (0)