Skip to content

Commit 8edde0f

Browse files
committed
Refactor: Replace redundant error mappings, update iter handling, and improve type safety
- Simplified repetitive `.map_err(|e| Error::X(e))` patterns using direct `Error::X`. - Replaced manual iteration bounds with `.iter()` and `iter().take(..).flatten()` for clarity. - Updated struct and enum defaults using `#[derive(Default)]` and `#[default]` attributes. - Boxed large structs (`Box<LocalSandbox>` etc.) to optimize memory usage. - Applied additional Clippy allowances (`#[allow(clippy::too_many_arguments, clippy::type_complexity)]`) for select modules. - Migrated to new iterator patterns and conforming ranges (`a..b` replaced with `.contains(&x)` logic).
1 parent d63646e commit 8edde0f

File tree

15 files changed

+74
-115
lines changed

15 files changed

+74
-115
lines changed

src/daemon.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ fn event_with_env(
391391
e
392392
}
393393

394+
#[allow(clippy::too_many_arguments)]
394395
fn event_skill(
395396
run_id: &str,
396397
event_type: &str,
@@ -499,9 +500,7 @@ fn plan_events_from_spec(run_id: &str, environment_id: &str, spec: &RunSpec) ->
499500
}
500501

501502
fn split_skill(raw: &str) -> Option<(&str, &str)> {
502-
let mut p = raw.splitn(2, ':');
503-
let t = p.next()?;
504-
let id = p.next()?;
503+
let (t, id) = raw.split_once(':')?;
505504
Some((t, id))
506505
}
507506

src/devices/virtio_net.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl VirtioNetDevice {
264264
mmio::STATUS => self.status,
265265
mmio::CONFIG_GENERATION => self.config_generation,
266266
// Device config (MAC address at offset 0x100)
267-
o if o >= mmio::CONFIG && o < mmio::CONFIG + 6 => {
267+
o if (mmio::CONFIG..mmio::CONFIG + 6).contains(&o) => {
268268
let idx = (o - mmio::CONFIG) as usize;
269269
self.mac[idx] as u32
270270
}

src/devices/virtio_vsock_mmio.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl VirtioVsockMmio {
140140
})
141141
}
142142

143+
#[allow(clippy::type_complexity)]
143144
fn setup_vhost(
144145
cid: u32,
145146
require_vhost: bool,
@@ -194,11 +195,9 @@ impl VirtioVsockMmio {
194195
kick[i] = Some(f);
195196
}
196197
Err(e) => {
197-
for j in 0..i {
198-
if let Some(f) = kick[j] {
199-
unsafe {
200-
libc::close(f);
201-
}
198+
for f in kick.iter().take(i).flatten() {
199+
unsafe {
200+
libc::close(*f);
202201
}
203202
}
204203
unsafe {
@@ -214,18 +213,14 @@ impl VirtioVsockMmio {
214213
call[i] = Some(f);
215214
}
216215
Err(e) => {
217-
for j in 0..=i {
218-
if let Some(f) = kick[j] {
219-
unsafe {
220-
libc::close(f);
221-
}
216+
for f in kick.iter().take(i + 1).flatten() {
217+
unsafe {
218+
libc::close(*f);
222219
}
223220
}
224-
for j in 0..i {
225-
if let Some(f) = call[j] {
226-
unsafe {
227-
libc::close(f);
228-
}
221+
for f in call.iter().take(i).flatten() {
222+
unsafe {
223+
libc::close(*f);
229224
}
230225
}
231226
unsafe {
@@ -339,7 +334,7 @@ impl VirtioVsockMmio {
339334
.map_err(|e| Error::Device(format!("get_host_address: {}", e)))?;
340335
let reg = unsafe { &mut *regions_ptr.add(i) };
341336
reg.guest_phys_addr = region.start_addr().raw_value();
342-
reg.memory_size = region.len() as u64;
337+
reg.memory_size = region.len();
343338
reg.userspace_addr = host_addr as u64;
344339
}
345340

@@ -363,6 +358,7 @@ impl VirtioVsockMmio {
363358
Ok(())
364359
}
365360

361+
#[allow(clippy::too_many_arguments)]
366362
fn set_vring(
367363
&self,
368364
index: u32,
@@ -501,7 +497,7 @@ impl VirtioVsockMmio {
501497
mmio::INTERRUPT_STATUS => self.interrupt_status,
502498
mmio::STATUS => self.status,
503499
mmio::CONFIG_GENERATION => self.config_generation,
504-
o if o >= mmio::CONFIG && o < mmio::CONFIG + 8 => {
500+
o if (mmio::CONFIG..mmio::CONFIG + 8).contains(&o) => {
505501
let off = (o - mmio::CONFIG) as usize;
506502
let cid64 = self.cid as u64;
507503
if off == 0 {
@@ -676,18 +672,14 @@ impl Drop for VirtioVsockMmio {
676672
libc::close(fd);
677673
}
678674
}
679-
for fd in &self.kick_eventfds {
680-
if let Some(f) = fd {
681-
unsafe {
682-
libc::close(*f);
683-
}
675+
for f in self.kick_eventfds.iter().flatten() {
676+
unsafe {
677+
libc::close(*f);
684678
}
685679
}
686-
for fd in &self._call_eventfds {
687-
if let Some(f) = fd {
688-
unsafe {
689-
libc::close(*f);
690-
}
680+
for f in self._call_eventfds.iter().flatten() {
681+
unsafe {
682+
libc::close(*f);
691683
}
692684
}
693685
}

src/llm.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ const SLIRP_GATEWAY: &str = "10.0.2.2";
4646
///
4747
/// Determines which LLM service the agent talks to. The provider is
4848
/// translated into environment variables injected into the guest VM.
49-
#[derive(Debug, Clone)]
49+
#[derive(Debug, Default, Clone)]
5050
pub enum LlmProvider {
5151
/// Anthropic Claude API (default).
5252
///
5353
/// Requires `ANTHROPIC_API_KEY` in the host environment.
5454
/// No extra env vars are injected.
55+
#[default]
5556
Claude,
5657

5758
/// Local Ollama instance running on the host.
@@ -81,12 +82,6 @@ pub enum LlmProvider {
8182
},
8283
}
8384

84-
impl Default for LlmProvider {
85-
fn default() -> Self {
86-
LlmProvider::Claude
87-
}
88-
}
89-
9085
impl LlmProvider {
9186
// -- Constructors --
9287

src/network/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ impl TapDevice {
9999

100100
unsafe {
101101
// Copy name into the ifreq name field
102-
libc::strncpy(
103-
ifr.ifr_name.as_mut_ptr(),
104-
cname.as_ptr(),
105-
libc::IFNAMSIZ as usize,
106-
);
102+
libc::strncpy(ifr.ifr_name.as_mut_ptr(), cname.as_ptr(), libc::IFNAMSIZ);
107103

108104
// Set flags to create a TAP device without extra packet info header.
109105
ifr.ifr_ifru.ifru_flags = (libc::IFF_TAP | libc::IFF_NO_PI) as libc::c_short;

src/network/slirp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ impl SlirpStack {
871871
}
872872

873873
/// Build a TCP packet (free function to avoid borrow issues with &self methods)
874+
#[allow(clippy::too_many_arguments)]
874875
fn build_tcp_packet_static(
875876
src_ip: Ipv4Address,
876877
dst_ip: Ipv4Address,

src/observe/logs.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ use std::sync::Mutex;
1111
use std::time::SystemTime;
1212

1313
/// Log levels
14-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
14+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1515
pub enum LogLevel {
1616
Trace = 0,
1717
Debug = 1,
18+
#[default]
1819
Info = 2,
1920
Warn = 3,
2021
Error = 4,
2122
}
2223

23-
impl Default for LogLevel {
24-
fn default() -> Self {
25-
Self::Info
26-
}
27-
}
28-
2924
impl std::fmt::Display for LogLevel {
3025
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3126
match self {

src/observe/tracer.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,17 @@ impl SpanContext {
104104
}
105105

106106
/// Status of a span
107-
#[derive(Debug, Clone, PartialEq)]
107+
#[derive(Debug, Default, Clone, PartialEq)]
108108
pub enum SpanStatus {
109109
/// Unset status
110+
#[default]
110111
Unset,
111112
/// Operation completed successfully
112113
Ok,
113114
/// Operation failed with error
114115
Error(String),
115116
}
116117

117-
impl Default for SpanStatus {
118-
fn default() -> Self {
119-
Self::Unset
120-
}
121-
}
122-
123118
/// A span representing a unit of work
124119
#[derive(Debug, Clone)]
125120
pub struct Span {

src/pipeline.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl PipelineResult {
9393
/// A single stage in a pipeline — either one Box or multiple Boxes in parallel.
9494
enum PipelineStage {
9595
/// A single Box executed sequentially.
96-
Single(VoidBox),
96+
Single(Box<VoidBox>),
9797
/// Multiple Boxes executed in parallel (fan-out). Their outputs are merged
9898
/// as a JSON array for the next stage.
9999
Parallel(Vec<VoidBox>),
@@ -110,21 +110,21 @@ impl Pipeline {
110110
pub fn from(first: VoidBox) -> Self {
111111
Self {
112112
name: first.name.clone(),
113-
stages: vec![PipelineStage::Single(first)],
113+
stages: vec![PipelineStage::Single(Box::new(first))],
114114
}
115115
}
116116

117117
/// Create a named pipeline from a single Box.
118118
pub fn named(name: impl Into<String>, first: VoidBox) -> Self {
119119
Self {
120120
name: name.into(),
121-
stages: vec![PipelineStage::Single(first)],
121+
stages: vec![PipelineStage::Single(Box::new(first))],
122122
}
123123
}
124124

125125
/// Pipe the output of the previous stage into the next Box.
126126
pub fn pipe(mut self, next: VoidBox) -> Self {
127-
self.stages.push(PipelineStage::Single(next));
127+
self.stages.push(PipelineStage::Single(Box::new(next)));
128128
self
129129
}
130130

src/sandbox/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ pub struct Sandbox {
8888

8989
enum SandboxInner {
9090
/// Local KVM-based sandbox
91-
Local(LocalSandbox),
91+
Local(Box<LocalSandbox>),
9292
/// Mock sandbox for testing
93-
Mock(MockSandbox),
93+
Mock(Box<MockSandbox>),
9494
}
9595

9696
impl Sandbox {
@@ -481,11 +481,11 @@ impl SandboxBuilder {
481481
let inner = match self.sandbox_type {
482482
SandboxType::Local => {
483483
let local = LocalSandbox::new(self.config.clone())?;
484-
SandboxInner::Local(local)
484+
SandboxInner::Local(Box::new(local))
485485
}
486486
SandboxType::Mock => {
487487
let mock = MockSandbox::new(self.config.clone());
488-
SandboxInner::Mock(mock)
488+
SandboxInner::Mock(Box::new(mock))
489489
}
490490
};
491491

@@ -590,7 +590,7 @@ impl MockSandbox {
590590
// - plan emits one JSON-like line
591591
// - apply reads stdin and echoes summary
592592
// - prompt mode (-p ... --output-format stream-json) returns deterministic JSONL
593-
let first = args.first().map(|s| *s).unwrap_or("");
593+
let first = args.first().copied().unwrap_or("");
594594
if first == "-p" {
595595
let output_format = args
596596
.windows(2)

0 commit comments

Comments
 (0)