From 380aec34d4799b0d8e6fcd232fdb74fa361e2275 Mon Sep 17 00:00:00 2001 From: Daniel Olano Date: Fri, 14 Jun 2024 17:50:07 +0200 Subject: [PATCH] Address Scales clippy errors Minor break: rename len method to size --- .github/workflows/scales-pr.yml | 25 ++++++++++++ .github/workflows/{test.yml => sube-pr.yml} | 0 scales/justfile | 3 ++ scales/src/serializer.rs | 45 ++++++++++----------- scales/src/value.rs | 36 ++++++++--------- sube/src/lib.rs | 2 +- 6 files changed, 69 insertions(+), 42 deletions(-) create mode 100644 .github/workflows/scales-pr.yml rename .github/workflows/{test.yml => sube-pr.yml} (100%) diff --git a/.github/workflows/scales-pr.yml b/.github/workflows/scales-pr.yml new file mode 100644 index 0000000..2798890 --- /dev/null +++ b/.github/workflows/scales-pr.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/sube-pr.yml similarity index 100% rename from .github/workflows/test.yml rename to .github/workflows/sube-pr.yml diff --git a/scales/justfile b/scales/justfile index 1456ed8..23f119f 100644 --- a/scales/justfile +++ b/scales/justfile @@ -5,3 +5,6 @@ check: lint: cargo clippy -- -D warnings + +test: + cargo test diff --git a/scales/src/serializer.rs b/scales/src/serializer.rs index 273283b..fd0efcd 100644 --- a/scales/src/serializer.rs +++ b/scales/src/serializer.rs @@ -295,9 +295,9 @@ where Ok(()) } - fn serialize_some(self, value: &T) -> Result + fn serialize_some(self, value: &T) -> Result where - T: Serialize, + T: Serialize + ?Sized, { self.out.put_u8(0x01); value.serialize(self) @@ -323,15 +323,15 @@ where (variant_index as u8).serialize(self) } - fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> Result + fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> Result where - T: Serialize, + T: Serialize + ?Sized, { self.maybe_some()?; value.serialize(self) } - fn serialize_newtype_variant( + fn serialize_newtype_variant( self, __name: &'static str, variant_index: u32, @@ -339,7 +339,7 @@ where value: &T, ) -> Result where - T: Serialize, + T: Serialize + ?Sized, { self.maybe_some()?; self.out.put_u8(variant_index as u8); @@ -519,7 +519,6 @@ where } } -/// #[derive(Debug)] pub enum TypedSerializer<'a, 'reg, B> where @@ -583,9 +582,9 @@ where type Ok = (); type Error = Error; - fn serialize_key(&mut self, key: &T) -> Result<()> + fn serialize_key(&mut self, key: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { match self { TypedSerializer::Enum(ser) => { @@ -604,9 +603,9 @@ where } } - fn serialize_value(&mut self, value: &T) -> Result<()> + fn serialize_value(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { match self { TypedSerializer::Composite(ser, types) => { @@ -647,9 +646,9 @@ where type Ok = (); type Error = Error; - fn serialize_element(&mut self, value: &T) -> Result<()> + fn serialize_element(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { match self { TypedSerializer::Composite(ser, types) => { @@ -683,9 +682,9 @@ where type Ok = (); type Error = Error; - fn serialize_field(&mut self, _key: &'static str, value: &T) -> Result<()> + fn serialize_field(&mut self, _key: &'static str, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { value.serialize(self.serializer()) } @@ -702,9 +701,9 @@ where type Ok = (); type Error = Error; - fn serialize_field(&mut self, _key: &'static str, value: &T) -> Result<()> + fn serialize_field(&mut self, _key: &'static str, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { value.serialize(self.serializer()) } @@ -721,9 +720,9 @@ where type Ok = (); type Error = Error; - fn serialize_element(&mut self, value: &T) -> Result<()> + fn serialize_element(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { value.serialize(self.serializer()) } @@ -740,9 +739,9 @@ where type Ok = (); type Error = Error; - fn serialize_field(&mut self, value: &T) -> Result<()> + fn serialize_field(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { value.serialize(self.serializer()) } @@ -759,9 +758,9 @@ where type Ok = (); type Error = Error; - fn serialize_field(&mut self, value: &T) -> Result<()> + fn serialize_field(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { value.serialize(self.serializer()) } diff --git a/scales/src/value.rs b/scales/src/value.rs index ae0c298..c06ba27 100644 --- a/scales/src/value.rs +++ b/scales/src/value.rs @@ -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) } @@ -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::(), @@ -58,7 +58,7 @@ impl<'a> Value<'a> { Primitive::Bool => mem::size_of::(), Primitive::Char => mem::size_of::(), Primitive::Str => { - let (l, p_size) = sequence_len(data); + let (l, p_size) = sequence_size(data); l + p_size } _ => unimplemented!(), @@ -66,7 +66,7 @@ impl<'a> Value<'a> { 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 @@ -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!(), } } @@ -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))?; @@ -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))?; @@ -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, @@ -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(), diff --git a/sube/src/lib.rs b/sube/src/lib.rs index 0302894..3d41d88 100644 --- a/sube/src/lib.rs +++ b/sube/src/lib.rs @@ -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 } })