Skip to content

Commit

Permalink
Cleanup generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal committed Jan 6, 2025
1 parent d5d5003 commit 3582bd1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 31 deletions.
3 changes: 2 additions & 1 deletion examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,8 @@ mod tests {

let call = &calls[0];
let fn_selector = call.decode_fn_selector()?;
let decoded_args = abi_formatter.decode_fn_args(&fn_selector, call.encoded_args.as_slice())?;
let decoded_args =
abi_formatter.decode_fn_args(&fn_selector, call.encoded_args.as_slice())?;

eprintln!(
"The script called: {fn_selector}({})",
Expand Down
11 changes: 8 additions & 3 deletions packages/fuels-core/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ mod tests {
};
}

assert!(try_from_bytes::<bool, _>(bytes.as_slice(), DecoderConfig::default())?);
assert!(try_from_bytes::<bool, _>(
bytes.as_slice(),
DecoderConfig::default()
)?);

test_decode!(u8, u16, u32, u64);

Expand All @@ -60,7 +63,8 @@ mod tests {
fn convert_bytes_into_tuple() -> Result<()> {
let tuple_in_bytes = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2];

let the_tuple: (u64, u32) = try_from_bytes(tuple_in_bytes.as_slice(), DecoderConfig::default())?;
let the_tuple: (u64, u32) =
try_from_bytes(tuple_in_bytes.as_slice(), DecoderConfig::default())?;

assert_eq!(the_tuple, (1, 2));

Expand Down Expand Up @@ -111,7 +115,8 @@ mod tests {
.unwrap();

// when
let decoded = try_from_bytes::<Test, _>(encoded.as_slice(), DecoderConfig::default()).unwrap();
let decoded =
try_from_bytes::<Test, _>(encoded.as_slice(), DecoderConfig::default()).unwrap();

// then
assert_eq!(decoded, input);
Expand Down
10 changes: 8 additions & 2 deletions packages/fuels-core/src/codec/abi_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ impl ABIDecoder {
///
/// assert_eq!(debug_string, format!("{expected_value}"));
/// ```
pub fn decode_as_debug_str(&self, param_type: &ParamType, mut bytes: impl Read) -> Result<String> {
pub fn decode_as_debug_str(
&self,
param_type: &ParamType,
mut bytes: impl Read,
) -> Result<String> {
let token = BoundedDecoder::new(self.config).decode(param_type, &mut bytes)?;
decode_as_debug_str(param_type, &token)
}
Expand Down Expand Up @@ -653,7 +657,9 @@ mod tests {
}
})
.for_each(|param_type| {
ABIDecoder::new(config).decode(&param_type, data.as_slice()).unwrap();
ABIDecoder::new(config)
.decode(&param_type, data.as_slice())
.unwrap();
})
}

Expand Down
41 changes: 23 additions & 18 deletions packages/fuels-core/src/codec/abi_decoder/bounded_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Read, iter::repeat, marker::PhantomData, str};
use std::{io::Read, iter::repeat, str};

use crate::{
codec::{
Expand All @@ -14,14 +14,12 @@ use crate::{

/// Is used to decode bytes into `Token`s from which types implementing `Tokenizable` can be
/// instantiated. Implements decoding limits to control resource usage.
pub(crate) struct BoundedDecoder<R> {
pub(crate) struct BoundedDecoder {
depth_tracker: CounterWithLimit,
token_tracker: CounterWithLimit,
/// We use a struct-level generic type to avoid https://github.com/rust-lang/rust/issues/50043
_read: PhantomData<R>,
}

impl<R: Read> BoundedDecoder<R> {
impl BoundedDecoder {
pub(crate) fn new(config: DecoderConfig) -> Self {
let depth_tracker =
CounterWithLimit::new(config.max_depth, "depth", CodecDirection::Decoding);
Expand All @@ -30,15 +28,18 @@ impl<R: Read> BoundedDecoder<R> {
Self {
depth_tracker,
token_tracker,
_read: PhantomData,
}
}

pub(crate) fn decode(&mut self, param_type: &ParamType, bytes: &mut R) -> Result<Token> {
pub(crate) fn decode<R: Read>(
&mut self,
param_type: &ParamType,
bytes: &mut R,
) -> Result<Token> {
self.decode_param(param_type, bytes)
}

pub(crate) fn decode_multiple(
pub(crate) fn decode_multiple<R: Read>(
&mut self,
param_types: &[ParamType],
bytes: &mut R,
Expand All @@ -57,7 +58,7 @@ impl<R: Read> BoundedDecoder<R> {
res
}

fn decode_param(&mut self, param_type: &ParamType, bytes: &mut R) -> Result<Token> {
fn decode_param<R: Read>(&mut self, param_type: &ParamType, bytes: &mut R) -> Result<Token> {
self.token_tracker.increase()?;
match param_type {
ParamType::Unit => Ok(Token::Unit),
Expand Down Expand Up @@ -93,13 +94,13 @@ impl<R: Read> BoundedDecoder<R> {
}
}

fn decode_std_string(bytes: &mut R) -> Result<Token> {
fn decode_std_string<R: Read>(bytes: &mut R) -> Result<Token> {
let data = decode_slice(bytes)?;
let string = str::from_utf8(&data)?.to_string();
Ok(Token::String(string))
}

fn decode_string_array(bytes: &mut R, length: usize) -> Result<Token> {
fn decode_string_array<R: Read>(bytes: &mut R, length: usize) -> Result<Token> {
let data = decode_sized(bytes, length)?;
let decoded = str::from_utf8(&data)?.to_string();
Ok(Token::StringArray(StaticStringToken::new(
Expand All @@ -108,17 +109,17 @@ impl<R: Read> BoundedDecoder<R> {
)))
}

fn decode_string_slice(bytes: &mut R) -> Result<Token> {
fn decode_string_slice<R: Read>(bytes: &mut R) -> Result<Token> {
let data = decode_slice(bytes)?;
let decoded = str::from_utf8(&data)?.to_string();
Ok(Token::StringSlice(StaticStringToken::new(decoded, None)))
}

fn decode_tuple(&mut self, param_types: &[ParamType], bytes: &mut R) -> Result<Token> {
fn decode_tuple<R: Read>(&mut self, param_types: &[ParamType], bytes: &mut R) -> Result<Token> {
Ok(Token::Tuple(self.decode_params(param_types, bytes)?))
}

fn decode_array(
fn decode_array<R: Read>(
&mut self,
param_type: &ParamType,
bytes: &mut R,
Expand All @@ -129,20 +130,24 @@ impl<R: Read> BoundedDecoder<R> {
))
}

fn decode_vector(&mut self, param_type: &ParamType, bytes: &mut R) -> Result<Token> {
fn decode_vector<R: Read>(&mut self, param_type: &ParamType, bytes: &mut R) -> Result<Token> {
let length = decode_len(bytes)?;
Ok(Token::Vector(
self.decode_params(repeat(param_type).take(length), bytes)?,
))
}

fn decode_struct(&mut self, fields: &[NamedParamType], bytes: &mut R) -> Result<Token> {
fn decode_struct<R: Read>(
&mut self,
fields: &[NamedParamType],
bytes: &mut R,
) -> Result<Token> {
Ok(Token::Struct(
self.decode_params(fields.iter().map(|(_, pt)| pt), bytes)?,
))
}

fn decode_enum(
fn decode_enum<R: Read>(
&mut self,
enum_variants: &EnumVariants,
bytes: &mut R,
Expand All @@ -159,7 +164,7 @@ impl<R: Read> BoundedDecoder<R> {
))))
}

fn decode_params<'a>(
fn decode_params<'a, R: Read>(
&mut self,
param_types: impl IntoIterator<Item = &'a ParamType>,
bytes: &mut R,
Expand Down
18 changes: 12 additions & 6 deletions packages/fuels-core/src/codec/abi_decoder/decode_as_debug_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ mod tests {

assert_eq!(
format!("{:?}", 1024u64),
decoder.decode_as_debug_str(&u64::param_type(), [0, 0, 0, 0, 0, 0, 4, 0].as_slice())?
decoder
.decode_as_debug_str(&u64::param_type(), [0, 0, 0, 0, 0, 0, 4, 0].as_slice())?
);

assert_eq!(
Expand All @@ -148,7 +149,8 @@ mod tests {
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 8, 0
].as_slice()
]
.as_slice()
)?
);
}
Expand All @@ -166,7 +168,8 @@ mod tests {
[
239, 134, 175, 169, 105, 108, 240, 220, 99, 133, 226, 196, 7, 166, 225, 89,
161, 16, 60, 239, 183, 226, 174, 6, 54, 251, 51, 211, 203, 42, 158, 74
].as_slice()
]
.as_slice()
)?
);

Expand All @@ -178,7 +181,8 @@ mod tests {
0, 0, 0, 0, 0, 0, 0, 32, 239, 134, 175, 169, 105, 108, 240, 220, 99, 133,
226, 196, 7, 166, 225, 89, 161, 16, 60, 239, 183, 226, 174, 6, 54, 251, 51,
211, 203, 42, 158, 74
].as_slice()
]
.as_slice()
)?
);

Expand All @@ -190,7 +194,8 @@ mod tests {
0, 0, 0, 0, 0, 0, 0, 32, 239, 134, 175, 169, 105, 108, 240, 220, 99, 133,
226, 196, 7, 166, 225, 89, 161, 16, 60, 239, 183, 226, 174, 6, 54, 251, 51,
211, 203, 42, 158, 74
].as_slice()
]
.as_slice()
)?
);

Expand All @@ -201,7 +206,8 @@ mod tests {
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 166, 225, 89, 161, 16, 60, 239, 183,
226, 174, 6, 54, 251, 51, 211, 203, 42, 158, 74
].as_slice()
]
.as_slice()
)?
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/fuels-core/src/codec/abi_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ mod tests {
let decoder = ABIFormatter::from_abi(UnifiedProgramABI::default()).unwrap();

// when
let err = decoder.decode_fn_args("non_existent_fn", [].as_slice()).unwrap_err();
let err = decoder
.decode_fn_args("non_existent_fn", [].as_slice())
.unwrap_err();

// then
let Error::Codec(err) = err else {
Expand Down

0 comments on commit 3582bd1

Please sign in to comment.