Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Feb 5, 2025
1 parent 0b5fb24 commit 63774c6
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions platform/rust/src/tile_server_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cxx::{CxxString, UniquePtr};

use crate::ffi;

/// Configuration options for a tile server.
pub struct TileServerOptions(UniquePtr<ffi::TileServerOptions>);

/// Convert a C string pointer to the Rust &CStr with lifetime of &self.
Expand All @@ -19,6 +20,11 @@ unsafe fn to_c_str(value: &CxxString) -> &CStr {
CStr::from_ptr(value.as_ptr().cast())
}

/// Convert an optional C string to a pointer.
fn opt_to_ptr(value: Option<&CString>) -> *const c_char {
value.map(|v| v.as_ptr()).unwrap_or(std::ptr::null())
}

impl TileServerOptions {
/// Convert a pointer to a C string to an optional Rust &CStr with lifetime of &self.
///
Expand Down Expand Up @@ -81,15 +87,11 @@ impl TileServerOptions {
) -> &mut Self {
unsafe {
// Slightly inefficient because CString allocation is than copied to std::string
let version_prefix = version_prefix
.as_ref()
.map(|v| v.as_ptr())
.unwrap_or(std::ptr::null());
ffi::TileServerOptions_withSourceTemplate(
self.0.pin_mut(),
source_template,
domain_name,
version_prefix,
opt_to_ptr(version_prefix.as_ref()),
);
}
self
Expand Down Expand Up @@ -120,15 +122,11 @@ impl TileServerOptions {
) -> &mut Self {
unsafe {
// Slightly inefficient because CString allocation is than copied to std::string
let version_prefix = version_prefix
.as_ref()
.map(|v| v.as_ptr())
.unwrap_or(std::ptr::null());
ffi::TileServerOptions_withStyleTemplate(
self.0.pin_mut(),
style_template,
domain_name,
version_prefix,
opt_to_ptr(version_prefix.as_ref()),
);
}
self
Expand Down Expand Up @@ -159,15 +157,11 @@ impl TileServerOptions {
) -> &mut Self {
unsafe {
// Slightly inefficient because CString allocation is than copied to std::string
let version_prefix = version_prefix
.as_ref()
.map(|v| v.as_ptr())
.unwrap_or(std::ptr::null());
ffi::TileServerOptions_withSpritesTemplate(
self.0.pin_mut(),
sprites_template,
domain_name,
version_prefix,
opt_to_ptr(version_prefix.as_ref()),
);
}
self
Expand Down Expand Up @@ -198,15 +192,11 @@ impl TileServerOptions {
) -> &mut Self {
unsafe {
// Slightly inefficient because CString allocation is than copied to std::string
let version_prefix = version_prefix
.as_ref()
.map(|v| v.as_ptr())
.unwrap_or(std::ptr::null());
ffi::TileServerOptions_withGlyphsTemplate(
self.0.pin_mut(),
glyphs_template,
domain_name,
version_prefix,
opt_to_ptr(version_prefix.as_ref()),
);
}
self
Expand Down Expand Up @@ -238,15 +228,11 @@ impl TileServerOptions {
) -> &mut Self {
unsafe {
// Slightly inefficient because CString allocation is than copied to std::string
let version_prefix = version_prefix
.as_ref()
.map(|v| v.as_ptr())
.unwrap_or(std::ptr::null());
ffi::TileServerOptions_withTileTemplate(
self.0.pin_mut(),
tile_template,
domain_name,
version_prefix,
opt_to_ptr(version_prefix.as_ref()),
);
}
self
Expand Down Expand Up @@ -343,6 +329,12 @@ impl Debug for TileServerOptions {
}
}

impl Default for TileServerOptions {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;
Expand Down

0 comments on commit 63774c6

Please sign in to comment.