Skip to content

Commit

Permalink
Cargo clippy changes
Browse files Browse the repository at this point in the history
Signed-off-by: Teddy Astie <[email protected]>
  • Loading branch information
TSnake41 committed Jan 15, 2025
1 parent 2a32206 commit 5ad9cc0
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 64 deletions.
24 changes: 8 additions & 16 deletions external/xen/src/hypercall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,18 @@ pub trait XenHypercall: Sized {
self.hypercall1(cmd, 0)
}

fn make_const_object<T: Copy + ?Sized>(
&self,
buffer: &T,
) -> Result<impl XenConstBuffer<T>, Self::Error>;
fn make_const_object<T: Copy>(&self, buffer: &T)
-> Result<impl XenConstBuffer<T>, Self::Error>;

fn make_mut_buffer<T: Copy + ?Sized>(
&self,
buffer: &mut T,
) -> Result<impl XenMutBuffer<T>, Self::Error>;
fn make_mut_buffer<T: Copy>(&self, buffer: &mut T)
-> Result<impl XenMutBuffer<T>, Self::Error>;

// Slices needs some special handling as they are not Copy themselves
// and a pointer to a slice doesn't point to its first element.

fn make_const_slice<T: Copy + Sized>(
&self,
slice: &[T],
) -> Result<impl XenConstBuffer<T>, Self::Error>;
fn make_const_slice<T: Copy>(&self, slice: &[T])
-> Result<impl XenConstBuffer<T>, Self::Error>;

fn make_mut_slice<T: Copy + Sized>(
&self,
slice: &mut [T],
) -> Result<impl XenMutBuffer<T>, Self::Error>;
fn make_mut_slice<T: Copy>(&self, slice: &mut [T])
-> Result<impl XenMutBuffer<T>, Self::Error>;
}
18 changes: 9 additions & 9 deletions external/xen/src/hypercall/unix/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl UnixXenHypercall {
})
} else {
// Get the number of page to hold the object layout.
let page_count = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
let page_count = size.div_ceil(PAGE_SIZE);
let length = NonZeroUsize::new(page_count * PAGE_SIZE)
.expect("Invalid size to page count convertion");

Expand Down Expand Up @@ -159,26 +159,26 @@ impl<T> Drop for XenCallBuffer<'_, T> {
}
}

pub struct UnixConstXenBuffer<'a, 'hyp, T: Copy + ?Sized> {
pub struct UnixConstXenBuffer<'a, 'hyp, T: Copy> {
// As const objects are actually being copied they actually don't
// need to hold a reference to their original counterpart.
// Use a PhantomData to make the borrow checker happy.
pub(super) original: PhantomData<&'a T>,
pub(super) buffer: XenCallBuffer<'hyp, T>,
}

pub struct UnixMutXenBuffer<'a, 'hyp, T: Copy + ?Sized> {
pub struct UnixMutXenBuffer<'a, 'hyp, T: Copy> {
pub(super) original: &'a mut T,
pub(super) buffer: XenCallBuffer<'hyp, T>,
}

impl<T: Copy + ?Sized> XenConstBuffer<T> for UnixConstXenBuffer<'_, '_, T> {
impl<T: Copy> XenConstBuffer<T> for UnixConstXenBuffer<'_, '_, T> {
fn as_hypercall_ptr(&self) -> *const T {
self.buffer.ptr.as_ptr()
}
}

impl<T: Copy + ?Sized> XenMutBuffer<T> for UnixMutXenBuffer<'_, '_, T> {
impl<T: Copy> XenMutBuffer<T> for UnixMutXenBuffer<'_, '_, T> {
fn as_hypercall_ptr(&mut self) -> *mut T {
self.buffer.ptr.as_ptr()
}
Expand All @@ -189,26 +189,26 @@ impl<T: Copy + ?Sized> XenMutBuffer<T> for UnixMutXenBuffer<'_, '_, T> {
}
}

pub struct UnixConstXenSlice<'a, 'hyp, T: Copy + ?Sized> {
pub struct UnixConstXenSlice<'a, 'hyp, T: Copy> {
// As const objects are actually being copied they actually don't
// need to hold a reference to their original counterpart.
// Use a PhantomData to make the compiler happy.
pub original: PhantomData<&'a [T]>,
pub buffer: XenCallBuffer<'hyp, T>,
}

pub struct UnixMutXenSlice<'a, 'b, T: Copy + ?Sized> {
pub struct UnixMutXenSlice<'a, 'b, T: Copy> {
pub original: &'a mut [T],
pub buffer: XenCallBuffer<'b, T>,
}

impl<T: Copy + ?Sized> XenConstBuffer<T> for UnixConstXenSlice<'_, '_, T> {
impl<T: Copy> XenConstBuffer<T> for UnixConstXenSlice<'_, '_, T> {
fn as_hypercall_ptr(&self) -> *const T {
self.buffer.ptr.as_ptr()
}
}

impl<T: Copy + ?Sized> XenMutBuffer<T> for UnixMutXenSlice<'_, '_, T> {
impl<T: Copy> XenMutBuffer<T> for UnixMutXenSlice<'_, '_, T> {
fn as_hypercall_ptr(&mut self) -> *mut T {
self.buffer.ptr.as_ptr()
}
Expand Down
13 changes: 5 additions & 8 deletions external/xen/src/hypercall/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ const PATH_HYPERCALL: &str = "/dev/xen/hypercall";
impl UnixXenHypercall {
pub fn new() -> Result<Self, io::Error> {
Ok(Self {
privcmd_device: File::options()
.read(true)
.write(true)
.open(PATH_PRIVCMD)?,
privcmd_device: File::options().read(true).write(true).open(PATH_PRIVCMD)?,
hypercall_device: File::options()
.read(true)
.write(true)
Expand Down Expand Up @@ -91,7 +88,7 @@ impl XenHypercall for UnixXenHypercall {
}
}

fn make_const_object<'a, T: Copy + ?Sized>(
fn make_const_object<'a, T: Copy>(
&self,
buffer: &'a T,
) -> Result<impl XenConstBuffer<T>, Self::Error> {
Expand All @@ -104,7 +101,7 @@ impl XenHypercall for UnixXenHypercall {
})
}

fn make_mut_buffer<T: Copy + ?Sized>(
fn make_mut_buffer<T: Copy>(
&'_ self,
buffer: &mut T,
) -> Result<impl XenMutBuffer<T>, Self::Error> {
Expand All @@ -117,7 +114,7 @@ impl XenHypercall for UnixXenHypercall {
})
}

fn make_const_slice<'a, T: Copy + Sized>(
fn make_const_slice<'a, T: Copy>(
&self,
slice: &'a [T],
) -> Result<impl XenConstBuffer<T>, Self::Error> {
Expand All @@ -130,7 +127,7 @@ impl XenHypercall for UnixXenHypercall {
})
}

fn make_mut_slice<T: Copy + Sized>(
fn make_mut_slice<T: Copy>(
&self,
slice: &mut [T],
) -> Result<impl XenMutBuffer<T>, Self::Error> {
Expand Down
20 changes: 6 additions & 14 deletions plugins/xcp-metrics-plugin-xen/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ impl PluginState {
stream: &mut UnixStream,
(kind, mut metric): (PluginMetricKind, Metric),
) -> anyhow::Result<()> {
let domain_metrics = self
.domid_metrics
.entry(domid.0)
.or_insert_with(|| HashMap::new());
let domain_metrics = self.domid_metrics.entry(domid.0).or_default();

let family_name = kind.family_name.to_compact_string();

let &mut uuid = domain_metrics.entry(kind).or_insert_with(|| Uuid::new_v4());
let &mut uuid = domain_metrics.entry(kind).or_insert_with(Uuid::new_v4);

// Inject domain UUID label into metric.
let mut labels = metric.labels.into_vec();
Expand All @@ -109,10 +106,7 @@ impl PluginState {
(kind, metric): (PluginMetricKind, Metric),
) -> anyhow::Result<()> {
let family_name = kind.family_name.to_compact_string();
let &mut uuid = self
.host_metrics
.entry(kind)
.or_insert_with(|| Uuid::new_v4());
let &mut uuid = self.host_metrics.entry(kind).or_insert_with(Uuid::new_v4);

stream.send_message(ProtocolMessage::UpdateMetric(UpdateMetric {
family_name,
Expand All @@ -133,7 +127,7 @@ pub fn run_plugin(stream: &mut UnixStream, hyp: &UnixXenHypercall) -> anyhow::Re
PCpuFreq.into(),
];

for xen_metric in metrics.as_ref() {
for xen_metric in metrics.iter() {
xen_metric.make_families(stream)?;
}

Expand All @@ -148,8 +142,7 @@ pub fn run_plugin(stream: &mut UnixStream, hyp: &UnixXenHypercall) -> anyhow::Re
// Get host metrics
for metric in metrics
.iter_mut()
.map(|xen_metric| xen_metric.read_host_metrics(physinfo, hyp))
.flatten()
.flat_map(|xen_metric| xen_metric.read_host_metrics(physinfo, hyp))
{
tracing::debug!("Pushing {metric:?}");
state.push_host_metric(stream, metric)?;
Expand All @@ -161,8 +154,7 @@ pub fn run_plugin(stream: &mut UnixStream, hyp: &UnixXenHypercall) -> anyhow::Re

for metric in metrics
.iter_mut()
.map(|xen_metric| xen_metric.read_domain_metrics(domain, hyp))
.flatten()
.flat_map(|xen_metric| xen_metric.read_domain_metrics(domain, hyp))
{
tracing::debug!("Pushing {metric:?}");
state.push_domain_metric((domid, dom_uuid), stream, metric)?;
Expand Down
4 changes: 2 additions & 2 deletions plugins/xcp-metrics-plugin-xenstore/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ pub async fn run_plugin(
let &mut uuid = state
.metrics_map
.entry(domid)
.or_insert_with(|| HashMap::new())
.or_insert_with(HashMap::new)
.entry(handler.family_name().into())
.or_insert_with(|| Uuid::new_v4());
.or_insert_with(Uuid::new_v4);

stream
.send_message_async(ProtocolMessage::UpdateMetric(UpdateMetric {
Expand Down
4 changes: 2 additions & 2 deletions plugins/xcp-metrics-plugin-xenstore/src/plugin/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl MetricHandler for MemInfoTotal {
return None;
}

let mut mem_total = xs.read(&path).await.ok()?.parse().ok()?;
let mut mem_total = xs.read(path).await.ok()?.parse().ok()?;
mem_total *= 1024; // KiB to bytes

Some(Metric {
Expand All @@ -54,7 +54,7 @@ impl MetricHandler for MemInfoFree {
return None;
}

let mut mem_total = xs.read(&path).await.ok()?.parse().ok()?;
let mut mem_total = xs.read(path).await.ok()?.parse().ok()?;
mem_total *= 1024; // KiB to bytes

Some(Metric {
Expand Down
14 changes: 7 additions & 7 deletions xcp-metrics-common/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//! TODO: Protocol negociation
use std::io::{self, Read, Write};

use serde::{Deserialize, Serialize};
use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use crate::metrics::{Metric, MetricType};
Expand Down Expand Up @@ -80,8 +80,8 @@ pub trait XcpMetricsStream {
fn recv_message(&mut self) -> io::Result<ProtocolMessage> {
let buffer = self.recv_message_raw()?;

Ok(ciborium::from_reader(buffer.as_ref())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?)
ciborium::from_reader(buffer.as_ref())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
}
}

Expand All @@ -91,7 +91,7 @@ where
{
fn send_message_raw(&mut self, message: &[u8]) -> io::Result<()> {
self.write_all(&(message.len() as u32).to_be_bytes())?;
self.write_all(&message)?;
self.write_all(message)?;

Ok(())
}
Expand Down Expand Up @@ -133,8 +133,8 @@ pub trait XcpMetricsAsyncStream {
async fn recv_message_async(&mut self) -> io::Result<ProtocolMessage> {
let buffer = self.recv_message_raw_async().await?;

Ok(ciborium::from_reader(buffer.as_ref())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?)
ciborium::from_reader(buffer.as_ref())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
}
}

Expand All @@ -144,7 +144,7 @@ where
{
async fn send_message_raw_async(&mut self, message: &[u8]) -> io::Result<()> {
self.write_u32(message.len() as u32).await?;
self.write_all(&message).await?;
self.write_all(message).await?;

Ok(())
}
Expand Down
11 changes: 5 additions & 6 deletions xcp-metrics-common/src/utils/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ pub struct MetricSetDelta<'a> {
/// New added families (name only)
pub added_families: Vec<(&'a str, &'a MetricFamily)>,

/// Changed family metadata
// /// Changed family metadata
// changed_families: Vec<&'a str>,

/// Metrics that no longer contain a family.
/// In case they reappears, they will need to be registered again.
pub orphaned_families: Vec<CompactString>,
Expand Down Expand Up @@ -179,14 +178,14 @@ impl MetricSetModel {
// Combine family name with each family metric.
.flat_map(|(name, family)| iter::zip(iter::repeat(name), family.metrics.iter()))
// Only consider metrics we don't have, and generate a new proper UUID.
.filter_map(|(name, (&uuid, metric))| {
.filter(|&(name, (_, metric))| {
// Due to contains_key expecting a tuple, we need to provide it a proper tuple (by cloning).
// TODO: Find a better solution than cloning.
(!self
!self
.metrics_map
.contains_key(&(name.clone(), metric.labels.clone())))
.then(|| (name.as_ref(), metric, uuid))
.contains_key(&(name.clone(), metric.labels.clone()))
})
.map(|(name, (&uuid, metric))| (name.as_ref(), metric, uuid))
.collect();

// Check for families that doesn't exist anymore.
Expand Down

0 comments on commit 5ad9cc0

Please sign in to comment.