Skip to content

Commit 0d591be

Browse files
committed
Changed to string functions to avoid copies where possible
1 parent c8caeb0 commit 0d591be

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn main() {
5454
.whitelist_function("sid_to_constellation")
5555
.whitelist_function("code_to_constellation")
5656
.whitelist_function("constellation_to_sat_count")
57+
.whitelist_function("constellation_to_string")
5758
.whitelist_function("code_to_sig_count")
5859
.whitelist_function("code_to_chip_count")
5960
.whitelist_function("code_to_chip_rate")

src/signal.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! satellite can send out multiple signals.
66
77
use crate::c_bindings;
8+
use std::borrow::Cow;
89
use std::ffi;
910
use std::str::Utf8Error;
1011

@@ -41,10 +42,23 @@ impl Constellation {
4142
}
4243
}
4344

45+
pub(crate) fn to_constellation_t(&self) -> c_bindings::constellation_t {
46+
*self as c_bindings::constellation_t
47+
}
48+
4449
/// Gets the specified maximum number of active satellites for the constellation
4550
pub fn sat_count(&self) -> u16 {
4651
unsafe { c_bindings::constellation_to_sat_count(*self as c_bindings::constellation_t) }
4752
}
53+
54+
pub fn to_str(&self) -> Cow<'static, str> {
55+
let c_str = unsafe {
56+
ffi::CStr::from_ptr(c_bindings::constellation_to_string(
57+
self.to_constellation_t(),
58+
))
59+
};
60+
c_str.to_string_lossy()
61+
}
4862
}
4963

5064
/// Code identifiers
@@ -150,7 +164,7 @@ pub enum Code {
150164
}
151165

152166
impl Code {
153-
fn from_code_t(value: c_bindings::code_t) -> Option<Code> {
167+
pub(crate) fn from_code_t(value: c_bindings::code_t) -> Option<Code> {
154168
match value {
155169
c_bindings::code_e_CODE_GPS_L1CA => Some(Code::GpsL1ca),
156170
c_bindings::code_e_CODE_GPS_L2CM => Some(Code::GpsL2cm),
@@ -225,11 +239,9 @@ impl Code {
225239
Self::from_code_t(unsafe { c_bindings::code_string_to_enum(s.as_ptr()) })
226240
}
227241

228-
/// Makes a string representation of a `Code`
229-
pub fn to_string(&self) -> Result<String, Utf8Error> {
242+
pub fn to_str(&self) -> Cow<'static, str> {
230243
let c_str = unsafe { ffi::CStr::from_ptr(c_bindings::code_to_string(self.to_code_t())) };
231-
232-
Ok(c_str.to_str()?.to_owned())
244+
c_str.to_string_lossy()
233245
}
234246

235247
/// Gets the corresponding `Constellation`

0 commit comments

Comments
 (0)