Skip to content

Commit

Permalink
fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Nov 27, 2024
1 parent e76e6c8 commit 92594a3
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 67 deletions.
81 changes: 33 additions & 48 deletions core/src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,11 @@ impl serde::Serializer for Serializer {
}

#[inline]
fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T: ser::Serialize + ?Sized>(
self,
name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: ser::Serialize,
{
) -> Result<Self::Ok, Self::Error> {
let ipld = value.serialize(self);
if name == CID_SERDE_PRIVATE_IDENTIFIER {
if let Ok(Ipld::Bytes(bytes)) = ipld {
Expand All @@ -227,16 +224,13 @@ impl serde::Serializer for Serializer {
ipld
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T: ser::Serialize + ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: ser::Serialize,
{
) -> Result<Self::Ok, Self::Error> {
let values = BTreeMap::from([(variant.to_owned(), value.serialize(self)?)]);
Ok(Self::Ok::Map(values))
}
Expand All @@ -247,10 +241,10 @@ impl serde::Serializer for Serializer {
}

#[inline]
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: ser::Serialize,
{
fn serialize_some<T: ser::Serialize + ?Sized>(
self,
value: &T,
) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

Expand Down Expand Up @@ -342,10 +336,10 @@ impl ser::SerializeSeq for SerializeVec {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
fn serialize_element<T: ser::Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error> {
self.vec.push(value.serialize(Serializer)?);
Ok(())
}
Expand All @@ -359,10 +353,10 @@ impl ser::SerializeTuple for SerializeVec {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
fn serialize_element<T: ser::Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error> {
ser::SerializeSeq::serialize_element(self, value)
}

Expand All @@ -375,10 +369,10 @@ impl ser::SerializeTupleStruct for SerializeVec {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
fn serialize_field<T: ser::Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error> {
ser::SerializeSeq::serialize_element(self, value)
}

Expand All @@ -391,10 +385,10 @@ impl ser::SerializeTupleVariant for SerializeTupleVariant {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
fn serialize_field<T: ser::Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error> {
self.vec.push(value.serialize(Serializer)?);
Ok(())
}
Expand All @@ -409,10 +403,7 @@ impl ser::SerializeMap for SerializeMap {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
fn serialize_key<T: ser::Serialize + ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> {
match key.serialize(Serializer)? {
Ipld::String(string_key) => {
self.next_key = Some(string_key);
Expand All @@ -422,10 +413,10 @@ impl ser::SerializeMap for SerializeMap {
}
}

fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
fn serialize_value<T: ser::Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error> {
let key = self.next_key.take();
// Panic because this indicates a bug in the program rather than an
// expected failure.
Expand All @@ -443,14 +434,11 @@ impl ser::SerializeStruct for SerializeMap {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_field<T: ?Sized>(
fn serialize_field<T: ser::Serialize + ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
) -> Result<(), Self::Error> {
serde::ser::SerializeMap::serialize_key(self, key)?;
serde::ser::SerializeMap::serialize_value(self, value)
}
Expand All @@ -464,14 +452,11 @@ impl ser::SerializeStructVariant for SerializeStructVariant {
type Ok = Ipld;
type Error = SerdeError;

fn serialize_field<T: ?Sized>(
fn serialize_field<T: ser::Serialize + ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
) -> Result<(), Self::Error> {
self.map
.insert(key.to_string(), value.serialize(Serializer)?);
Ok(())
Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dependency_on_unit_never_type_fallback)]

use libipld::cbor::DagCborCodec;
use libipld::codec::assert_roundtrip;
use libipld::{ipld, DagCbor, Ipld};
Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/examples/name_attr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dependency_on_unit_never_type_fallback)]

use libipld::cbor::DagCborCodec;
use libipld::codec::{Decode, Encode};
use libipld::ipld::Ipld;
Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/examples/renamed-package/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dependency_on_unit_never_type_fallback)]

//! The purpose of this example is to test whether the derive compiles if the libipld package was
//! renamed in the `Cargo.toml` file.
use ipld::DagCbor;
Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/examples/repr_attr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dependency_on_unit_never_type_fallback)]

use libipld::cbor::DagCborCodec;
use libipld::codec::assert_roundtrip;
use libipld::{ipld, DagCbor};
Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;

Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/tests/struct.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dependency_on_unit_never_type_fallback)]

use libipld::cbor::{DagCbor, DagCborCodec};
use libipld::codec::assert_roundtrip;
use libipld::{ipld, DagCbor};
Expand Down
2 changes: 2 additions & 0 deletions dag-cbor-derive/tests/union.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dependency_on_unit_never_type_fallback)]

use libipld::cbor::DagCborCodec;
use libipld::codec::assert_roundtrip;
use libipld::{ipld, DagCbor};
Expand Down
10 changes: 5 additions & 5 deletions dag-cbor/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn write_u8<W: Write>(w: &mut W, major: MajorKind, value: u8) -> Result<()>

/// Writes a u16 to a cbor encoded byte stream.
pub fn write_u16<W: Write>(w: &mut W, major: MajorKind, value: u16) -> Result<()> {
if value <= u16::from(u8::max_value()) {
if value <= u16::from(u8::MAX) {
write_u8(w, major, value as u8)?;
} else {
let mut buf = [(major as u8) << 5 | 25, 0, 0];
Expand All @@ -50,7 +50,7 @@ pub fn write_u16<W: Write>(w: &mut W, major: MajorKind, value: u16) -> Result<()

/// Writes a u32 to a cbor encoded byte stream.
pub fn write_u32<W: Write>(w: &mut W, major: MajorKind, value: u32) -> Result<()> {
if value <= u32::from(u16::max_value()) {
if value <= u32::from(u16::MAX) {
write_u16(w, major, value as u16)?;
} else {
let mut buf = [(major as u8) << 5 | 26, 0, 0, 0, 0];
Expand All @@ -62,7 +62,7 @@ pub fn write_u32<W: Write>(w: &mut W, major: MajorKind, value: u32) -> Result<()

/// Writes a u64 to a cbor encoded byte stream.
pub fn write_u64<W: Write>(w: &mut W, major: MajorKind, value: u64) -> Result<()> {
if value <= u64::from(u32::max_value()) {
if value <= u64::from(u32::MAX) {
write_u32(w, major, value as u32)?;
} else {
let mut buf = [(major as u8) << 5 | 27, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down Expand Up @@ -224,12 +224,12 @@ impl Encode<DagCbor> for String {
impl Encode<DagCbor> for i128 {
fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
if *self < 0 {
if -(*self + 1) > u64::max_value() as i128 {
if -(*self + 1) > u64::MAX as i128 {
return Err(NumberOutOfRange::new::<i128>().into());
}
write_u64(w, MajorKind::NegativeInt, -(*self + 1) as u64)?;
} else {
if *self > u64::max_value() as i128 {
if *self > u64::MAX as i128 {
return Err(NumberOutOfRange::new::<i128>().into());
}
write_u64(w, MajorKind::UnsignedInt, *self as u64)?;
Expand Down
7 changes: 2 additions & 5 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,11 @@ impl<S: StoreParams> Block<S> {
}

/// Encode a block.`
pub fn encode<CE: Codec, T: Encode<CE> + ?Sized>(
pub fn encode<CE: Codec + Into<S::Codecs>, T: Encode<CE> + ?Sized>(
codec: CE,
hcode: S::Hashes,
payload: &T,
) -> Result<Self>
where
CE: Into<S::Codecs>,
{
) -> Result<Self> {
debug_assert_eq!(
Into::<u64>::into(codec),
Into::<u64>::into(Into::<S::Codecs>::into(codec))
Expand Down
12 changes: 3 additions & 9 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,9 @@ impl From<String> for Path {
}
}

impl ToString for Path {
fn to_string(&self) -> String {
let mut path = "".to_string();
for seg in &self.0 {
path.push_str(seg.as_str());
path.push('/');
}
path.pop();
path
impl std::fmt::Display for Path {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_slice().join("/"))
}
}

Expand Down

0 comments on commit 92594a3

Please sign in to comment.