Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up new warnings and lints #90

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ The `value-bag` crate is no-std by default, and offers the following Cargo featu
- `serde`: Enable support for using the [`serde`](https://github.com/serde-rs/serde) serialization framework for inspecting `ValueBag`s by implementing `serde::Serialize`. Implies `std` and `serde1`.
- `serde1`: Enable support for the stable `1.x.x` version of `serde`.
- `owned`: Add support for buffering `ValueBag`s into an owned `Send + Sync` variant.
- `seq`: Add support for working with values that are serialized as sequences.
- `seq`: Add support for working with sequences without needing to go through a full serialization framework.
- `test`: Add test helpers for inspecting the shape of the value inside a `ValueBag`.
13 changes: 5 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ impl Error {
pub(crate) fn try_boxed(msg: &'static str, e: impl fmt::Display) -> Self {
#[cfg(feature = "std")]
{
use crate::std::string::ToString;

let _ = msg;
Error::boxed(e.to_string())
Error::boxed(format!("{msg}: {e}"))
}
#[cfg(not(feature = "std"))]
{
Expand All @@ -42,11 +39,11 @@ impl Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Inner::*;
match &self.inner {
match self.inner {
#[cfg(feature = "std")]
&Boxed(ref err) => err.fmt(f),
&Msg(ref msg) => msg.fmt(f),
&Fmt => fmt::Error.fmt(f),
Boxed(ref err) => err.fmt(f),
Msg(ref msg) => msg.fmt(f),
Fmt => fmt::Error.fmt(f),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ mod tests {

impl Fill for TestFill {
fn fill(&self, slot: Slot) -> Result<(), Error> {
let dbg: &dyn fmt::Debug = &1;
let dbg = &1 as &dyn fmt::Debug;

slot.fill_debug(&dbg)
slot.fill_debug(dbg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ where
{
#[inline]
fn from(v: &'v &'u str) -> Self {
ValueBag::from_str(*v)
ValueBag::from_str(v)
}
}

Expand Down
47 changes: 23 additions & 24 deletions src/internal/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<'v> Internal<'v> {
Internal::Float(value) => Cast::Float(*value),
Internal::Bool(value) => Cast::Bool(*value),
Internal::Char(value) => Cast::Char(*value),
Internal::Str(value) => Cast::Str(*value),
Internal::Str(value) => Cast::Str(value),
Internal::None => Cast::None,
other => {
// If the erased value isn't a primitive then we visit it
Expand Down Expand Up @@ -534,8 +534,6 @@ mod tests {

use super::*;

use crate::std::string::ToString;

use crate::test::IntoValueBag;

#[test]
Expand All @@ -549,7 +547,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn primitive_capture_str() {
let s: &str = &"short lived".to_string();
let s: &str = "short lived";
assert_eq!(
"short lived",
ValueBag::try_capture(s)
Expand Down Expand Up @@ -618,48 +616,51 @@ mod tests {

assert_eq!(
-1i64,
-1i8.into_value_bag()
-(1i8
.into_value_bag()
.by_ref()
.to_i64()
.expect("invalid value")
.expect("invalid value"))
);
assert_eq!(
-1i64,
-1i8.into_value_bag()
-(1i8
.into_value_bag()
.by_ref()
.to_i64()
.expect("invalid value")
.expect("invalid value"))
);
assert_eq!(
-1i64,
-1i8.into_value_bag()
-(1i8
.into_value_bag()
.by_ref()
.to_i64()
.expect("invalid value")
.expect("invalid value"))
);
assert_eq!(
-1i64,
-1i64
-(1i64
.into_value_bag()
.by_ref()
.to_i64()
.expect("invalid value")
.expect("invalid value"))
);
assert_eq!(
-1i64,
-1isize
-(1isize
.into_value_bag()
.by_ref()
.to_i64()
.expect("invalid value")
.expect("invalid value"))
);
assert_eq!(
-1i128,
-1i128
-(1i128
.into_value_bag()
.by_ref()
.to_i128()
.expect("invalid value")
.expect("invalid value"))
);

assert!(1f64.into_value_bag().by_ref().to_f64().is_some());
Expand Down Expand Up @@ -688,20 +689,18 @@ mod tests {
.to_char()
.expect("invalid value")
);
assert_eq!(
true,
true.into_value_bag()
.by_ref()
.to_bool()
.expect("invalid value")
);
assert!(true
.into_value_bag()
.by_ref()
.to_bool()
.expect("invalid value"));
}

#[test]
fn as_cast() {
assert_eq!(1.0, 1f64.into_value_bag().as_f64());
assert_eq!(1.0, 1u64.into_value_bag().as_f64());
assert_eq!(-1.0, -1i64.into_value_bag().as_f64());
assert_eq!(-1.0, -(1i64.into_value_bag().as_f64()));
assert!(true.into_value_bag().as_f64().is_nan());
}
}
10 changes: 5 additions & 5 deletions src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl<'v> ValueBag<'v> {

impl<'v> Internal<'v> {
#[inline]
pub(crate) const fn by_ref<'u>(&'u self) -> Internal<'u> {
pub(crate) const fn by_ref(&self) -> Internal<'_> {
match self {
Internal::Signed(value) => Internal::Signed(*value),
Internal::Unsigned(value) => Internal::Unsigned(*value),
Expand All @@ -384,7 +384,7 @@ impl<'v> Internal<'v> {
Internal::Float(value) => Internal::Float(*value),
Internal::Bool(value) => Internal::Bool(*value),
Internal::Char(value) => Internal::Char(*value),
Internal::Str(value) => Internal::Str(*value),
Internal::Str(value) => Internal::Str(value),
Internal::None => Internal::None,

Internal::Fill(value) => Internal::Fill(*value),
Expand Down Expand Up @@ -456,9 +456,9 @@ impl<'v> Internal<'v> {
#[cfg(feature = "inline-i128")]
Internal::BigUnsigned(value) => visitor.u128(value),
#[cfg(not(feature = "inline-i128"))]
Internal::BigSigned(value) => visitor.borrowed_i128(*value),
Internal::BigSigned(value) => visitor.borrowed_i128(value),
#[cfg(not(feature = "inline-i128"))]
Internal::BigUnsigned(value) => visitor.borrowed_u128(*value),
Internal::BigUnsigned(value) => visitor.borrowed_u128(value),
Internal::Float(value) => visitor.f64(*value),
Internal::Bool(value) => visitor.bool(*value),
Internal::Char(value) => visitor.char(*value),
Expand Down Expand Up @@ -517,7 +517,7 @@ impl<'v> Internal<'v> {
#[cfg(all(feature = "seq", feature = "owned"))]
Internal::SharedRefSeq(value) => visitor.shared_seq(value),

Internal::Poisoned(msg) => visitor.poisoned(*msg),
Internal::Poisoned(msg) => visitor.poisoned(msg),
}
}
}
2 changes: 1 addition & 1 deletion src/internal/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) enum OwnedInternal {

impl OwnedInternal {
#[inline]
pub(crate) const fn by_ref<'v>(&'v self) -> Internal<'v> {
pub(crate) const fn by_ref(&self) -> Internal {
match self {
#[cfg(not(feature = "inline-i128"))]
OwnedInternal::BigSigned(v) => Internal::BigSigned(v),
Expand Down
21 changes: 10 additions & 11 deletions src/internal/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'v> ValueBag<'v> {
struct ExtendF64<S>(S);

impl<'a, S: Extend<f64>> ExtendValue<'a> for ExtendF64<S> {
fn extend<'b>(&mut self, inner: Internal<'b>) {
fn extend(&mut self, inner: Internal<'_>) {
self.0.extend(Some(ValueBag { inner }.as_f64()))
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ where
I: AsRef<[T]> + ?Sized + 'a,
&'a T: Into<ValueBag<'a>>,
{
fn visit<'v>(&self, visitor: &mut dyn Visitor<'v>) {
fn visit(&self, visitor: &mut dyn Visitor<'_>) {
for v in self.as_ref().iter() {
if let ControlFlow::Break(()) = visitor.element(v.into()) {
return;
Expand All @@ -223,15 +223,15 @@ where
}

pub(crate) trait Seq {
fn visit<'v>(&self, visitor: &mut dyn Visitor<'v>);
fn visit(&self, visitor: &mut dyn Visitor<'_>);

fn borrowed_visit<'v>(&'v self, visitor: &mut dyn Visitor<'v>) {
self.visit(visitor)
}
}

impl<'a, S: Seq + ?Sized> Seq for &'a S {
fn visit<'v>(&self, visitor: &mut dyn Visitor<'v>) {
fn visit(&self, visitor: &mut dyn Visitor<'_>) {
(**self).visit(visitor)
}

Expand Down Expand Up @@ -259,6 +259,8 @@ impl<'a, 'v, T: Visitor<'v> + ?Sized> Visitor<'v> for &'a mut T {
}

pub(crate) trait DowncastSeq {
// Currently only used when `owned` is also available
#[allow(dead_code)]
fn as_any(&self) -> &dyn Any;
fn as_super(&self) -> &dyn Seq;
}
Expand All @@ -274,7 +276,7 @@ impl<T: Seq + 'static> DowncastSeq for T {
}

impl<'a> Seq for dyn DowncastSeq + Send + Sync + 'a {
fn visit<'v>(&self, visitor: &mut dyn Visitor<'v>) {
fn visit(&self, visitor: &mut dyn Visitor<'_>) {
self.as_super().visit(visitor)
}
}
Expand Down Expand Up @@ -554,7 +556,7 @@ mod alloc_support {
struct ExtendStr<'a, S>(S, PhantomData<Cow<'a, str>>);

impl<'a, S: Extend<Option<Cow<'a, str>>>> ExtendValue<'a> for ExtendStr<'a, S> {
fn extend<'b>(&mut self, inner: Internal<'b>) {
fn extend(&mut self, inner: Internal<'_>) {
self.0.extend(Some(
ValueBag { inner }
.to_str()
Expand All @@ -576,16 +578,13 @@ mod alloc_support {
pub(crate) mod owned {
use super::*;

use crate::{
owned::OwnedValueBag,
std::{boxed::Box, vec::Vec},
};
use crate::{owned::OwnedValueBag, std::boxed::Box};

#[derive(Clone)]
pub(crate) struct OwnedSeq(Box<[OwnedValueBag]>);

impl Seq for OwnedSeq {
fn visit<'v>(&self, visitor: &mut dyn Visitor<'v>) {
fn visit(&self, visitor: &mut dyn Visitor<'_>) {
for item in self.0.iter() {
if let ControlFlow::Break(()) = visitor.element(item.by_ref()) {
return;
Expand Down
42 changes: 18 additions & 24 deletions src/internal/serde/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'v> value_bag_serde1::lib::Serialize for ValueBag<'v> {
};

self.internal_visit(&mut visitor)
.map_err(|e| S::Error::custom(e))?;
.map_err(S::Error::custom)?;

visitor.into_result()
}
Expand Down Expand Up @@ -279,7 +279,7 @@ fn serialize_seq<S: value_bag_serde1::lib::Serializer>(
s.serializer.end()
}

pub(crate) fn internal_visit<'v>(v: &dyn Serialize, visitor: &mut dyn InternalVisitor<'v>) -> bool {
pub(crate) fn internal_visit(v: &dyn Serialize, visitor: &mut dyn InternalVisitor<'_>) -> bool {
struct VisitorSerializer<'a, 'v>(&'a mut dyn InternalVisitor<'v>);

impl<'a, 'v> value_bag_serde1::lib::Serializer for VisitorSerializer<'a, 'v> {
Expand Down Expand Up @@ -587,10 +587,10 @@ pub(crate) mod seq {
Err(Unsupported)
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: value_bag_serde1::lib::Serialize,
{
fn serialize_some<T: value_bag_serde1::lib::Serialize + ?Sized>(
self,
value: &T,
) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

Expand All @@ -611,27 +611,21 @@ pub(crate) mod seq {
Err(Unsupported)
}

fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T: value_bag_serde1::lib::Serialize + ?Sized>(
self,
_: &'static str,
_: &T,
) -> Result<Self::Ok, Self::Error>
where
T: value_bag_serde1::lib::Serialize,
{
) -> Result<Self::Ok, Self::Error> {
Err(Unsupported)
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T: value_bag_serde1::lib::Serialize + ?Sized>(
self,
_: &'static str,
_: u32,
_: &'static str,
_: &T,
) -> Result<Self::Ok, Self::Error>
where
T: value_bag_serde1::lib::Serialize,
{
) -> Result<Self::Ok, Self::Error> {
Err(Unsupported)
}

Expand Down Expand Up @@ -689,10 +683,10 @@ pub(crate) mod seq {

type Error = Unsupported;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: value_bag_serde1::lib::Serialize,
{
fn serialize_element<T: value_bag_serde1::lib::Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Self::Error> {
self.0.extend(Internal::AnonSerde1(&value));
Ok(())
}
Expand All @@ -707,10 +701,10 @@ pub(crate) mod seq {

type Error = Unsupported;

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

Expand Down
Loading
Loading