Skip to content

Commit 2264071

Browse files
committed
Use Box<str> instead of String to promote immutability
1 parent cee90d3 commit 2264071

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

luomu-libpcap/src/error.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@ pub enum Error {
88
/// Loop terminated by pcap_breakloop (PCAP_ERROR_BREAK).
99
Break,
1010
/// The capture needs to be activated (PCAP_ERROR_NOT_ACTIVATED).
11-
NotActivated(String),
11+
NotActivated(Box<str>),
1212
/// Capture handle already activated (PCAP_ERROR_ACTIVATED).
13-
AlreadyActivated(String),
13+
AlreadyActivated(Box<str>),
1414
/// The capture source specified when the handle was created doesn't exist
1515
/// (PCAP_ERROR_NO_SUCH_DEVICE).
16-
NoSuchDevice(String),
16+
NoSuchDevice(Box<str>),
1717
/// Monitor mode was specified but the capture source doesn't support
1818
/// monitor mode (PCAP_ERROR_RFMON_NOTSUP).
19-
MonitorModeNotSupported(String),
19+
MonitorModeNotSupported(Box<str>),
2020
/// The operation is supported only in monitor mode (PCAP_ERROR_NOT_RFMON).
2121
OnlySupportedInMonitorMode,
2222
/// The process doesn't have permission to open the capture source
2323
/// (PCAP_ERROR_PERM_DENIED).
24-
PermissionDenied(String),
24+
PermissionDenied(Box<str>),
2525
/// The capture source device is not up (PCAP_ERROR_IFACE_NOT_UP).
26-
InterfaceNotUp(String),
26+
InterfaceNotUp(Box<str>),
2727
/// This device doesn't support setting the time stamp type
2828
/// (PCAP_ERROR_CANTSET_TSTAMP_TYPE).
29-
TimestampTypeNotSupported(String),
29+
TimestampTypeNotSupported(Box<str>),
3030
/// The process has permission to open the capture source but doesn't have
3131
/// permission to put it into promiscuous mode
3232
/// (PCAP_ERROR_PROMISC_PERM_DENIED).
33-
PromiscuousPermissionDenied(String),
33+
PromiscuousPermissionDenied(Box<str>),
3434
/// The requested time stamp precision is not supported
3535
/// (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP).
3636
TimestampPrecisionNotSupported,
3737

3838
/// Error from `libpcap`
39-
PcapError(String),
39+
PcapError(Box<str>),
4040
/// Warning from `libpcap`
41-
PcapWarning(String),
41+
PcapWarning(Box<str>),
4242
/// Unknown error code from `libpcap`.
4343
PcapErrorCode(i32),
4444

luomu-libpcap/src/functions.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub fn pcap_activate(pcap_t: &PcapT) -> Result<()> {
219219
// positive value on success with warnings, and a negative value on error. A
220220
// non-zero return value indicates what warning or error condition occurred.
221221
if ret > 0 {
222-
return Err(Error::PcapWarning(status_to_str(ret)?));
222+
return Err(Error::PcapWarning(status_to_str(ret)?.into()));
223223
}
224224

225225
Ok(())
@@ -238,7 +238,7 @@ pub fn get_error(pcap_t: &PcapT) -> Result<Error> {
238238
tracing::trace!("get_error({:p})", pcap_t.pcap_t);
239239
let ptr = unsafe { libpcap::pcap_geterr(pcap_t.pcap_t) };
240240
let cstr = unsafe { CStr::from_ptr(ptr) };
241-
let err = cstr.to_str()?.to_owned();
241+
let err = cstr.to_str()?.into();
242242
Ok(Error::PcapError(err))
243243
}
244244

@@ -657,22 +657,21 @@ fn get_interface_flags(val: u32) -> BTreeSet<InterfaceFlag> {
657657
flags
658658
}
659659

660-
fn status_to_str(error: libc::c_int) -> Result<String> {
660+
fn status_to_str<'a>(error: libc::c_int) -> Result<&'a str> {
661661
tracing::trace!("status_to_str({error})");
662662
let ptr = unsafe { libpcap::pcap_statustostr(error) };
663663
let cstr = unsafe { CStr::from_ptr(ptr) };
664-
let status = cstr.to_str()?.to_owned();
665-
Ok(status)
664+
Ok(cstr.to_str()?)
666665
}
667666

668667
/// Check for `libpcap` error.
669668
fn check_pcap_error(pcap_t: &PcapT, ret: i32) -> Result<()> {
670669
if tracing::event_enabled!(tracing::Level::TRACE) {
671670
let status = match ret {
672-
0 => "ok".to_string(),
673-
n => status_to_str(n).unwrap_or_default(),
671+
0 => "ok",
672+
n => status_to_str(n)?,
674673
};
675-
tracing::trace!("check_pcap_error({:p}, {ret}) = {status}", pcap_t.pcap_t,);
674+
tracing::trace!("check_pcap_error({:p}, {ret}) = {status}", pcap_t.pcap_t);
676675
}
677676

678677
match ret {

luomu-libpcap/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ unsafe impl Send for PcapT {}
4646
impl PcapT {
4747
/// get interface name
4848
///
49-
/// `get_interface` returns the interface name if known or "<unknown>".
50-
pub fn get_inteface(&self) -> String {
49+
/// `get_interface` returns the interface name if known or "\<unknown\>".
50+
pub fn get_inteface(&self) -> Box<str> {
5151
if let Some(name) = &self.interface {
52-
name.to_string()
52+
name.clone()
5353
} else {
54-
String::from("<unknown>")
54+
"<unknown>".into()
5555
}
5656
}
5757

@@ -203,9 +203,9 @@ impl Errbuf {
203203
Ok(cstr.to_str()?)
204204
}
205205

206-
/// Return libpcap's error message as String.
207-
fn as_string(&self) -> Result<String> {
208-
Ok(self.as_str()?.to_string())
206+
/// Return libpcap's error message as string.
207+
fn as_string(&self) -> Result<Box<str>> {
208+
Ok(self.as_str()?.into())
209209
}
210210

211211
/// Return libcap's error as [Error] type.
@@ -523,11 +523,11 @@ impl PcapIfT {
523523
}
524524

525525
/// Find capture device which have IP address `ip`.
526-
pub fn find_interface_with_ip(&self, ip: &IpAddr) -> Option<String> {
526+
pub fn find_interface_with_ip(&self, ip: &IpAddr) -> Option<Box<str>> {
527527
for interface in self.get_interfaces() {
528528
if interface.has_address(ip) {
529529
tracing::trace!("find_interface_with_ip({ip}) = {interface:?}");
530-
return Some(interface.name.into_string());
530+
return Some(interface.name.clone());
531531
}
532532
}
533533
None

0 commit comments

Comments
 (0)