Skip to content

Commit

Permalink
strings are strings after all
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed May 26, 2024
1 parent 4e452c0 commit 12674a1
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 202 deletions.
2 changes: 1 addition & 1 deletion examples/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod example {
for srv_result in srv_results {
println!(
"host: {} (port: {}), priority: {}, weight: {}",
srv_result.host().to_string_lossy(),
srv_result.host(),
srv_result.port(),
srv_result.priority(),
srv_result.weight()
Expand Down
11 changes: 4 additions & 7 deletions examples/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ mod example {
}
Ok(ref soa_result) => {
println!("Successful SOA lookup...");
println!(
"Name server: {}",
soa_result.name_server().to_string_lossy()
);
println!("Hostmaster: {}", soa_result.hostmaster().to_string_lossy());
println!("Name server: {}", soa_result.name_server());
println!("Hostmaster: {}", soa_result.hostmaster());
println!("Serial: {}", soa_result.serial());
println!("Retry: {}", soa_result.retry());
println!("Expire: {}", soa_result.expire());
Expand All @@ -42,10 +39,10 @@ mod example {
Ok(ref name_info_result) => {
println!("Successful name info lookup...");
if let Some(node) = name_info_result.node() {
println!("Node: {}", node.to_string_lossy());
println!("Node: {}", node);
}
if let Some(service) = name_info_result.service() {
println!("Service: {}", service.to_string_lossy());
println!("Service: {}", service);
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/caa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ impl<'a> CAAResult<'a> {
}

/// The property represented by this `CAAResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn property(self) -> &'a CStr {
unsafe { CStr::from_ptr(self.caa_reply.property.cast()) }
pub fn property(self) -> &'a str {
let c_str = unsafe { CStr::from_ptr(self.caa_reply.property.cast()) };
c_str.to_str().unwrap()
}

/// The value represented by this `CAAResult`.
Expand All @@ -120,11 +117,7 @@ impl<'a> CAAResult<'a> {
impl<'a> fmt::Display for CAAResult<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Critical: {}, ", self.critical())?;
write!(
fmt,
"Property: {}, ",
self.property().to_str().unwrap_or("<not utf8>")
)?;
write!(fmt, "Property: {}, ", self.property())?;
let value = str::from_utf8(self.value()).unwrap_or("<binary>");
write!(fmt, "Value: {}", value)
}
Expand Down
7 changes: 1 addition & 6 deletions src/cname.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ffi::CStr;
use std::fmt;
use std::os::raw::{c_int, c_uchar, c_void};
use std::ptr;
Expand Down Expand Up @@ -42,11 +41,7 @@ impl CNameResults {
}

/// Returns the hostname from this `CNameResults`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn hostname(&self) -> &CStr {
pub fn hostname(&self) -> &str {
self.hostent.hostname()
}

Expand Down
7 changes: 1 addition & 6 deletions src/host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ffi::CStr;
use std::fmt;
use std::os::raw::{c_int, c_void};

Expand All @@ -20,11 +19,7 @@ impl<'a> HostResults<'a> {
}

/// Returns the hostname from this `HostResults`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn hostname(self) -> &'a CStr {
pub fn hostname(self) -> &'a str {
self.hostent.hostname()
}

Expand Down
34 changes: 12 additions & 22 deletions src/hostent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use itertools::Itertools;
use crate::types::AddressFamily;
use crate::utils::address_family;

fn hostname(hostent: &c_types::hostent) -> &CStr {
unsafe { CStr::from_ptr(hostent.h_name.cast()) }
fn hostname(hostent: &c_types::hostent) -> &str {
let c_str = unsafe { CStr::from_ptr(hostent.h_name.cast()) };
c_str.to_str().unwrap()
}

fn addresses(hostent: &c_types::hostent) -> HostAddressResultsIter {
Expand All @@ -28,23 +29,17 @@ fn aliases(hostent: &c_types::hostent) -> HostAliasResultsIter {
}

fn display(hostent: &c_types::hostent, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"Hostname: {}, ",
hostname(hostent).to_str().unwrap_or("<not utf8>")
)?;
write!(fmt, "Hostname: {}, ", hostname(hostent))?;
let addresses = addresses(hostent).format(", ");
write!(fmt, "Addresses: [{addresses}], ")?;
let aliases = aliases(hostent)
.map(|cstr| cstr.to_str().unwrap_or("<not utf8>"))
.format(", ");
let aliases = aliases(hostent).format(", ");
write!(fmt, "Aliases: [{aliases}]")
}

pub trait HasHostent<'a>: Sized {
fn hostent(self) -> &'a c_types::hostent;

fn hostname(self) -> &'a CStr {
fn hostname(self) -> &'a str {
let hostent = self.hostent();
hostname(hostent)
}
Expand Down Expand Up @@ -171,28 +166,23 @@ impl<'a> Iterator for HostAddressResultsIter<'a> {
unsafe impl<'a> Send for HostAddressResultsIter<'a> {}
unsafe impl<'a> Sync for HostAddressResultsIter<'a> {}

/// Iterator of `&'a CStr`s.
///
/// Each item is very likely to be a valid UTF-8 string, but the underlying `c-ares` library does
/// not guarantee this - so we leave it to users to decide whether they prefer a fallible
/// conversion, a lossy conversion, or something else altogether.
/// Iterator of `&'a str`s.
#[derive(Clone, Copy, Debug)]
pub struct HostAliasResultsIter<'a> {
next: &'a *const c_char,
}

impl<'a> Iterator for HostAliasResultsIter<'a> {
type Item = &'a CStr;
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
let h_alias = *self.next;
if h_alias.is_null() {
None
} else {
unsafe {
self.next = &*ptr::from_ref(self.next).offset(1);
let c_str = CStr::from_ptr(h_alias);
Some(c_str)
}
self.next = unsafe { &*ptr::from_ref(self.next).offset(1) };
let c_str = unsafe { CStr::from_ptr(h_alias) };
let string = c_str.to_str().unwrap();
Some(string)
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/mx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,9 @@ unsafe impl<'a> Sync for MXResultsIter<'a> {}

impl<'a> MXResult<'a> {
/// Returns the hostname in this `MXResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn host(self) -> &'a CStr {
unsafe { CStr::from_ptr(self.mx_reply.host) }
pub fn host(self) -> &'a str {
let c_str = unsafe { CStr::from_ptr(self.mx_reply.host) };
c_str.to_str().unwrap()
}

/// Returns the priority from this `MXResult`.
Expand All @@ -115,11 +112,7 @@ impl<'a> MXResult<'a> {

impl<'a> fmt::Display for MXResult<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"Hostname: {}, ",
self.host().to_str().unwrap_or("<not utf8>")
)?;
write!(fmt, "Hostname: {}, ", self.host())?;
write!(fmt, "Priority: {}", self.priority())
}
}
Expand Down
32 changes: 12 additions & 20 deletions src/nameinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,27 @@ impl<'a> NameInfoResult<'a> {
}

/// Returns the node from this `NameInfoResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn node(&self) -> Option<&CStr> {
self.node.map(|string| unsafe { CStr::from_ptr(string) })
pub fn node(&self) -> Option<&str> {
self.node.map(|string| {
let c_str = unsafe { CStr::from_ptr(string) };
c_str.to_str().unwrap()
})
}

/// Returns the service from this `NameInfoResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn service(&self) -> Option<&CStr> {
self.service.map(|string| unsafe { CStr::from_ptr(string) })
pub fn service(&self) -> Option<&str> {
self.service.map(|string| {
let c_str = unsafe { CStr::from_ptr(string) };
c_str.to_str().unwrap()
})
}
}

impl<'a> fmt::Display for NameInfoResult<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let node = self
.node()
.map(|cstr| cstr.to_str().unwrap_or("<not utf8>"))
.unwrap_or("<None>");
let node = self.node().unwrap_or("<None>");
write!(fmt, "Node: {node}, ")?;
let service = self
.service()
.map(|cstr| cstr.to_str().unwrap_or("<not utf8>"))
.unwrap_or("<None>");
let service = self.service().unwrap_or("<None>");
write!(fmt, "Service: {service}")
}
}
Expand Down
60 changes: 16 additions & 44 deletions src/naptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,39 +99,27 @@ unsafe impl<'a> Sync for NAPTRResultsIter<'a> {}

impl<'a> NAPTRResult<'a> {
/// Returns the flags in this `NAPTRResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn flags(self) -> &'a CStr {
unsafe { CStr::from_ptr(self.naptr_reply.flags.cast()) }
pub fn flags(self) -> &'a str {
let c_str = unsafe { CStr::from_ptr(self.naptr_reply.flags.cast()) };
c_str.to_str().unwrap()
}

/// Returns the service name in this `NAPTRResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn service_name(self) -> &'a CStr {
unsafe { CStr::from_ptr(self.naptr_reply.service.cast()) }
pub fn service_name(self) -> &'a str {
let c_str = unsafe { CStr::from_ptr(self.naptr_reply.service.cast()) };
c_str.to_str().unwrap()
}

/// Returns the regular expression in this `NAPTRResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn reg_exp(self) -> &'a CStr {
unsafe { CStr::from_ptr(self.naptr_reply.regexp.cast()) }
pub fn reg_exp(self) -> &'a str {
let c_str = unsafe { CStr::from_ptr(self.naptr_reply.regexp.cast()) };
c_str.to_str().unwrap()
}

/// Returns the replacement pattern in this `NAPTRResult`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn replacement_pattern(self) -> &'a CStr {
unsafe { CStr::from_ptr(self.naptr_reply.replacement) }
pub fn replacement_pattern(self) -> &'a str {
let c_str = unsafe { CStr::from_ptr(self.naptr_reply.replacement) };
c_str.to_str().unwrap()
}

/// Returns the order value in this `NAPTRResult`.
Expand All @@ -147,26 +135,10 @@ impl<'a> NAPTRResult<'a> {

impl<'a> fmt::Display for NAPTRResult<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"Flags: {}, ",
self.flags().to_str().unwrap_or("<not utf8>")
)?;
write!(
fmt,
"Service name: {}, ",
self.service_name().to_str().unwrap_or("<not utf8>")
)?;
write!(
fmt,
"Regular expression: {}, ",
self.reg_exp().to_str().unwrap_or("<not utf8>")
)?;
write!(
fmt,
"Replacement pattern: {}, ",
self.replacement_pattern().to_str().unwrap_or("<not utf8>")
)?;
write!(fmt, "Flags: {}, ", self.flags())?;
write!(fmt, "Service name: {}, ", self.service_name())?;
write!(fmt, "Regular expression: {}, ", self.reg_exp())?;
write!(fmt, "Replacement pattern: {}, ", self.replacement_pattern())?;
write!(fmt, "Order: {}, ", self.order())?;
write!(fmt, "Preference: {}", self.preference())
}
Expand Down
18 changes: 3 additions & 15 deletions src/ns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ffi::CStr;
use std::fmt;
use std::os::raw::{c_int, c_uchar, c_void};
use std::ptr;
Expand Down Expand Up @@ -38,11 +37,7 @@ impl NSResults {
}

/// Returns the hostname from this `NSResults`.
///
/// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares`
/// library does not guarantee this - so we leave it to users to decide whether they prefer a
/// fallible conversion, a lossy conversion, or something else altogether.
pub fn hostname(&self) -> &CStr {
pub fn hostname(&self) -> &str {
self.hostent.hostname()
}

Expand All @@ -54,15 +49,8 @@ impl NSResults {

impl fmt::Display for NSResults {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"Hostname: {}, ",
self.hostname().to_str().unwrap_or("<not utf8>")
)?;
let aliases = self
.aliases()
.map(|cstr| cstr.to_str().unwrap_or("<not utf8>"))
.format(", ");
write!(fmt, "Hostname: {}, ", self.hostname())?;
let aliases = self.aliases().format(", ");
write!(fmt, "Aliases: [{aliases}]")
}
}
Expand Down
Loading

0 comments on commit 12674a1

Please sign in to comment.