Skip to content

Commit

Permalink
Address Scales clippy errors
Browse files Browse the repository at this point in the history
Minor break: rename len method to size
  • Loading branch information
olanod committed Jun 14, 2024
1 parent 40195cf commit 380aec3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 42 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/scales-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
pull_request:
paths: ['scales/**']

name: Scales

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: extractions/setup-just@v2
- name: Check
run: just -f scales/justfile check
- name: Lint
run: just -f scales/justfile lint

test:
runs-on: ubuntu-latest
needs: check
steps:
- uses: actions/checkout@v2
- uses: extractions/setup-just@v2
- name: Test
run: just -f scales/justfile test
File renamed without changes.
3 changes: 3 additions & 0 deletions scales/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ check:

lint:
cargo clippy -- -D warnings

test:
cargo test
45 changes: 22 additions & 23 deletions scales/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ where
Ok(())
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
fn serialize_some<T>(self, value: &T) -> Result<Self::Ok>
where
T: Serialize,
T: Serialize + ?Sized,
{
self.out.put_u8(0x01);
value.serialize(self)
Expand All @@ -323,23 +323,23 @@ where
(variant_index as u8).serialize(self)
}

fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> Result<Self::Ok>
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Self::Ok>
where
T: Serialize,
T: Serialize + ?Sized,
{
self.maybe_some()?;
value.serialize(self)
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T>(
self,
__name: &'static str,
variant_index: u32,
_variant: &'static str,
value: &T,
) -> Result<Self::Ok>
where
T: Serialize,
T: Serialize + ?Sized,
{
self.maybe_some()?;
self.out.put_u8(variant_index as u8);
Expand Down Expand Up @@ -519,7 +519,6 @@ where
}
}

///
#[derive(Debug)]
pub enum TypedSerializer<'a, 'reg, B>
where
Expand Down Expand Up @@ -583,9 +582,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()>
fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
match self {
TypedSerializer::Enum(ser) => {
Expand All @@ -604,9 +603,9 @@ where
}
}

fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<()>
fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
match self {
TypedSerializer::Composite(ser, types) => {
Expand Down Expand Up @@ -647,9 +646,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
match self {
TypedSerializer::Composite(ser, types) => {
Expand Down Expand Up @@ -683,9 +682,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
value.serialize(self.serializer())
}
Expand All @@ -702,9 +701,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
value.serialize(self.serializer())
}
Expand All @@ -721,9 +720,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
value.serialize(self.serializer())
}
Expand All @@ -740,9 +739,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
value.serialize(self.serializer())
}
Expand All @@ -759,9 +758,9 @@ where
type Ok = ();
type Error = Error;

fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where
T: Serialize,
T: Serialize + ?Sized,
{
value.serialize(self.serializer())
}
Expand Down
36 changes: 18 additions & 18 deletions scales/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> Value<'a> {
}

fn new_value(&self, data: &mut Bytes, ty_id: TypeId) -> Self {
let size = self.ty_len(data.chunk(), ty_id);
let size = self.ty_size(data.chunk(), ty_id);
Value::new(data.copy_to_bytes(size), ty_id, self.registry)
}

Expand All @@ -38,11 +38,11 @@ impl<'a> Value<'a> {
self.registry.resolve(ty).expect("in registry")
}

pub fn len(&self) -> usize {
self.ty_len(&self.data, self.ty_id)
pub fn size(&self) -> usize {
self.ty_size(&self.data, self.ty_id)
}

fn ty_len(&self, data: &[u8], ty: TypeId) -> usize {
fn ty_size(&self, data: &[u8], ty: TypeId) -> usize {
match &self.resolve(ty).type_def {
TypeDef::Primitive(ref p) => match p {
Primitive::U8 => mem::size_of::<u8>(),
Expand All @@ -58,15 +58,15 @@ impl<'a> Value<'a> {
Primitive::Bool => mem::size_of::<bool>(),
Primitive::Char => mem::size_of::<char>(),
Primitive::Str => {
let (l, p_size) = sequence_len(data);
let (l, p_size) = sequence_size(data);
l + p_size
}
_ => unimplemented!(),
},
TypeDef::Composite(c) => c
.fields
.iter()
.fold(0, |c, f| c + self.ty_len(&data[c..], f.ty.id)),
.fold(0, |c, f| c + self.ty_size(&data[c..], f.ty.id)),
TypeDef::Variant(e) => {
let var = e
.variants
Expand All @@ -79,20 +79,20 @@ impl<'a> Value<'a> {
} else {
var.fields
.iter()
.fold(1, |c, f| c + self.ty_len(&data[c..], f.ty.id))
.fold(1, |c, f| c + self.ty_size(&data[c..], f.ty.id))
}
}
TypeDef::Sequence(s) => {
let (len, prefix_size) = sequence_len(data);
let (len, prefix_size) = sequence_size(data);
let ty_id = s.type_param.id;
(0..len).fold(prefix_size, |c, _| c + self.ty_len(&data[c..], ty_id))
(0..len).fold(prefix_size, |c, _| c + self.ty_size(&data[c..], ty_id))
}
TypeDef::Array(a) => a.len.try_into().unwrap(),
TypeDef::Tuple(t) => t
.fields
.iter()
.fold(0, |c, f| c + self.ty_len(&data[c..], f.id)),
TypeDef::Compact(_) => compact_len(data),
.fold(0, |c, f| c + self.ty_size(&data[c..], f.id)),
TypeDef::Compact(_) => compact_size(data),
TypeDef::BitSequence(_) => unimplemented!(),
}
}
Expand Down Expand Up @@ -141,18 +141,18 @@ impl<'a> Serialize for Value<'a> {
}
}
Bytes(_) => {
let (_, s) = sequence_len(data.chunk());
let (_, s) = sequence_size(data.chunk());
data.advance(s);
ser.serialize_bytes(data.chunk())
}
Char => ser.serialize_char(char::from_u32(data.get_u32_le()).unwrap()),
Str => {
let (_, s) = sequence_len(data.chunk());
let (_, s) = sequence_size(data.chunk());
data.advance(s);
ser.serialize_str(str::from_utf8(data.chunk()).unwrap())
}
Sequence(ty) => {
let (len, p_size) = sequence_len(data.chunk());
let (len, p_size) = sequence_size(data.chunk());
data.advance(p_size);

let mut seq = ser.serialize_seq(Some(len))?;
Expand All @@ -162,7 +162,7 @@ impl<'a> Serialize for Value<'a> {
seq.end()
}
Map(ty_k, ty_v) => {
let (len, p_size) = sequence_len(data.chunk());
let (len, p_size) = sequence_size(data.chunk());
data.advance(p_size);

let mut state = ser.serialize_map(Some(len))?;
Expand Down Expand Up @@ -242,7 +242,7 @@ impl<'a> Serialize for Value<'a> {
}

#[inline]
fn compact_len(data: &[u8]) -> usize {
fn compact_size(data: &[u8]) -> usize {
match data[0] % 0b100 {
0 => 1,
1 => 2,
Expand All @@ -251,11 +251,11 @@ fn compact_len(data: &[u8]) -> usize {
}
}

fn sequence_len(data: &[u8]) -> (usize, usize) {
fn sequence_size(data: &[u8]) -> (usize, usize) {
// need to peek at the data to know the length of sequence
// first byte(s) gives us a hint of the(compact encoded) length
// https://substrate.dev/docs/en/knowledgebase/advanced/codec#compactgeneral-integers
let len = compact_len(data);
let len = compact_size(data);
(
match len {
1 => (data[0] >> 2).into(),
Expand Down
2 changes: 1 addition & 1 deletion sube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async fn query<'m>(chain: &impl Backend, meta: &'m Metadata, path: &str) -> Resu
*type_id,
&meta.types,
);
pointer += (value.len() * 2) + 32;
pointer += (value.size() * 2) + 32;
value
}
})
Expand Down

0 comments on commit 380aec3

Please sign in to comment.