Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary lifetimes #153

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions src/buffer_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use geos_sys::*;
use std::sync::Arc;

/// Contains the parameters which describe how a [Geometry](crate::Geometry) buffer should be constructed using [buffer_with_params](crate::Geom::buffer_with_params)
pub struct BufferParams<'a> {
pub struct BufferParams {
ptr: PtrWrap<*mut GEOSBufferParams>,
context: Arc<ContextHandle<'a>>,
context: Arc<ContextHandle>,
}

/// Build options for a [`BufferParams`] object
Expand All @@ -23,8 +23,8 @@ pub struct BufferParamsBuilder {
single_sided: Option<bool>,
}

impl<'a> BufferParams<'a> {
pub fn new() -> GResult<BufferParams<'a>> {
impl BufferParams {
pub fn new() -> GResult<BufferParams> {
match ContextHandle::init_e(Some("BufferParams::new")) {
Ok(context) => unsafe {
let ptr = GEOSBufferParams_create_r(context.as_raw());
Expand Down Expand Up @@ -164,51 +164,51 @@ impl<'a> BufferParams<'a> {
}
}

unsafe impl<'a> Send for BufferParams<'a> {}
unsafe impl<'a> Sync for BufferParams<'a> {}
unsafe impl Send for BufferParams {}
unsafe impl Sync for BufferParams {}

impl<'a> Drop for BufferParams<'a> {
impl Drop for BufferParams {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { GEOSBufferParams_destroy_r(self.get_raw_context(), self.as_raw_mut()) };
}
}
}

impl<'a> AsRaw for BufferParams<'a> {
impl AsRaw for BufferParams {
type RawType = GEOSBufferParams;

fn as_raw(&self) -> *const Self::RawType {
*self.ptr
}
}

impl<'a> AsRawMut for BufferParams<'a> {
impl AsRawMut for BufferParams {
type RawType = GEOSBufferParams;

unsafe fn as_raw_mut_override(&self) -> *mut Self::RawType {
*self.ptr
}
}

impl<'a> ContextInteractions<'a> for BufferParams<'a> {
fn set_context_handle(&mut self, context: ContextHandle<'a>) {
impl ContextInteractions for BufferParams {
fn set_context_handle(&mut self, context: ContextHandle) {
self.context = Arc::new(context);
}

fn get_context_handle(&self) -> &ContextHandle<'a> {
fn get_context_handle(&self) -> &ContextHandle {
&self.context
}
}

impl<'a> ContextHandling for BufferParams<'a> {
type Context = Arc<ContextHandle<'a>>;
impl ContextHandling for BufferParams {
type Context = Arc<ContextHandle>;

fn get_raw_context(&self) -> GEOSContextHandle_t {
self.context.as_raw()
}

fn clone_context(&self) -> Arc<ContextHandle<'a>> {
fn clone_context(&self) -> Arc<ContextHandle> {
Arc::clone(&self.context)
}
}
Expand All @@ -234,7 +234,7 @@ impl BufferParamsBuilder {
self.single_sided = Some(is_single_sided);
self
}
pub fn build(self) -> GResult<BufferParams<'static>> {
pub fn build(self) -> GResult<BufferParams> {
let mut params = BufferParams::new()?;
if let Some(style) = self.end_cap_style {
params.set_end_cap_style(style)?;
Expand Down
37 changes: 17 additions & 20 deletions src/context_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ use std::ops::Deref;
use std::slice;
use std::sync::Mutex;

pub type HandlerCallback<'a> = Box<dyn Fn(&str) + Send + Sync + 'a>;
pub type HandlerCallback = Box<dyn Fn(&str) + Send + Sync>;

macro_rules! set_callbacks {
($c_func:ident, $kind:ident, $callback_name:ident, $last:ident) => {
#[allow(clippy::needless_lifetimes)]
fn $kind<'a>(ptr: GEOSContextHandle_t, nf: *mut InnerContext<'a>) {
fn $kind(ptr: GEOSContextHandle_t, nf: *mut InnerContext) {
#[allow(clippy::extra_unused_lifetimes)]
unsafe extern "C" fn message_handler_func<'a>(
message: *const c_char,
data: *mut c_void,
) {
let inner_context: &InnerContext<'a> = &*(data as *mut _);
unsafe extern "C" fn message_handler_func(message: *const c_char, data: *mut c_void) {
let inner_context: &InnerContext = &*(data as *mut _);

if let Ok(callback) = inner_context.$callback_name.lock() {
let bytes = slice::from_raw_parts(message as *const u8, strlen(message));
Expand Down Expand Up @@ -65,19 +62,19 @@ impl<T> Deref for PtrWrap<T> {
unsafe impl<T> Send for PtrWrap<T> {}
unsafe impl<T> Sync for PtrWrap<T> {}

pub(crate) struct InnerContext<'a> {
pub(crate) struct InnerContext {
last_notification: Mutex<Option<String>>,
last_error: Mutex<Option<String>>,
notif_callback: Mutex<HandlerCallback<'a>>,
error_callback: Mutex<HandlerCallback<'a>>,
notif_callback: Mutex<HandlerCallback>,
error_callback: Mutex<HandlerCallback>,
}

pub struct ContextHandle<'a> {
pub struct ContextHandle {
ptr: PtrWrap<GEOSContextHandle_t>,
pub(crate) inner: PtrWrap<*mut InnerContext<'a>>,
pub(crate) inner: PtrWrap<*mut InnerContext>,
}

impl<'a> ContextHandle<'a> {
impl ContextHandle {
/// Creates a new `ContextHandle`.
///
/// # Example
Expand Down Expand Up @@ -105,8 +102,8 @@ impl<'a> ContextHandle<'a> {
let last_notification = Mutex::new(None);
let last_error = Mutex::new(None);

let notif_callback: Mutex<HandlerCallback<'a>> = Mutex::new(Box::new(|_| {}));
let error_callback: Mutex<HandlerCallback<'a>> = Mutex::new(Box::new(|_| {}));
let notif_callback: Mutex<HandlerCallback> = Mutex::new(Box::new(|_| {}));
let error_callback: Mutex<HandlerCallback> = Mutex::new(Box::new(|_| {}));

let inner = Box::into_raw(Box::new(InnerContext {
last_notification,
Expand All @@ -128,7 +125,7 @@ impl<'a> ContextHandle<'a> {
*self.ptr
}

pub(crate) fn get_inner(&self) -> &InnerContext<'a> {
pub(crate) fn get_inner(&self) -> &InnerContext {
unsafe { &*self.inner.0 }
}

Expand All @@ -145,7 +142,7 @@ impl<'a> ContextHandle<'a> {
///
/// context_handle.set_notice_message_handler(Some(Box::new(|s| println!("new message: {}", s))));
/// ```
pub fn set_notice_message_handler(&self, nf: Option<HandlerCallback<'a>>) {
pub fn set_notice_message_handler(&self, nf: Option<HandlerCallback>) {
let inner_context = self.get_inner();
if let Ok(mut callback) = inner_context.notif_callback.lock() {
if let Some(nf) = nf {
Expand All @@ -169,7 +166,7 @@ impl<'a> ContextHandle<'a> {
///
/// context_handle.set_error_message_handler(Some(Box::new(|s| println!("new message: {}", s))));
/// ```
pub fn set_error_message_handler(&self, ef: Option<HandlerCallback<'a>>) {
pub fn set_error_message_handler(&self, ef: Option<HandlerCallback>) {
let inner_context = self.get_inner();
if let Ok(mut callback) = inner_context.error_callback.lock() {
if let Some(ef) = ef {
Expand Down Expand Up @@ -304,14 +301,14 @@ impl<'a> ContextHandle<'a> {
}
}

impl<'a> Drop for ContextHandle<'a> {
impl Drop for ContextHandle {
fn drop(&mut self) {
unsafe {
if !self.ptr.is_null() {
GEOS_finish_r(self.as_raw());
}
// Now we just have to clear stuff!
let _inner: Box<InnerContext<'a>> = Box::from_raw(self.inner.0);
let _inner: Box<InnerContext> = Box::from_raw(self.inner.0);
}
}
}
50 changes: 25 additions & 25 deletions src/coord_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ type AsArrayOutput = (Vec<f64>, Vec<f64>, Option<Vec<f64>>, Option<Vec<f64>>);
/// coords.set_x(0, 10.);
/// assert_eq!(coords.get_x(0), Ok(10.));
/// ```
pub struct CoordSeq<'a> {
pub struct CoordSeq {
pub(crate) ptr: PtrWrap<*mut GEOSCoordSequence>,
pub(crate) context: Arc<ContextHandle<'a>>,
pub(crate) context: Arc<ContextHandle>,
nb_dimensions: usize,
nb_lines: usize,
}

impl<'a> CoordSeq<'a> {
impl CoordSeq {
/// Creates a new `CoordSeq`.
///
/// # Example
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<'a> CoordSeq<'a> {
/// }
/// assert_eq!(coord_seq2.get_x(1), Ok(1.));
/// ```
pub fn new(size: u32, dims: CoordDimensions) -> GResult<CoordSeq<'a>> {
pub fn new(size: u32, dims: CoordDimensions) -> GResult<CoordSeq> {
let context_handle = ContextHandle::init_e(Some("CoordSeq::new"))?;
unsafe {
let ptr = GEOSCoordSeq_create_r(context_handle.as_raw(), size, dims.into());
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'a> CoordSeq<'a> {
/// let x: &[f64] = &[];
/// assert!(CoordSeq::new_from_vec(&[x]).is_err());
/// ```
pub fn new_from_vec<T: AsRef<[f64]>>(data: &[T]) -> GResult<CoordSeq<'a>> {
pub fn new_from_vec<T: AsRef<[f64]>>(data: &[T]) -> GResult<CoordSeq> {
let size = data.len();

if size > 0 {
Expand Down Expand Up @@ -180,7 +180,7 @@ impl<'a> CoordSeq<'a> {
size: usize,
has_z: bool,
has_m: bool,
) -> GResult<CoordSeq<'a>> {
) -> GResult<CoordSeq> {
let mut dims: u32 = 2;
if has_z {
dims += 1;
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<'a> CoordSeq<'a> {
y: &[f64],
z: Option<&[f64]>,
m: Option<&[f64]>,
) -> GResult<CoordSeq<'a>> {
) -> GResult<CoordSeq> {
assert_eq!(x.len(), y.len(), "Arrays have different lengths.");

let mut dims: u32 = 2;
Expand Down Expand Up @@ -278,11 +278,11 @@ impl<'a> CoordSeq<'a> {

pub(crate) unsafe fn new_from_raw(
ptr: *mut GEOSCoordSequence,
context: Arc<ContextHandle<'a>>,
context: Arc<ContextHandle>,
size: u32,
dims: u32,
caller: &str,
) -> GResult<CoordSeq<'a>> {
) -> GResult<CoordSeq> {
if ptr.is_null() {
let extra = if let Some(x) = context.get_last_error() {
format!("\nLast error: {x}")
Expand Down Expand Up @@ -814,7 +814,7 @@ impl<'a> CoordSeq<'a> {
///
/// assert_eq!(geom.to_wkt().unwrap(), "POINT (1.0000000000000000 2.0000000000000000)");
/// ```
pub fn create_point(self) -> GResult<Geometry<'a>> {
pub fn create_point(self) -> GResult<Geometry> {
Geometry::create_point(self)
}

Expand All @@ -834,20 +834,20 @@ impl<'a> CoordSeq<'a> {
/// "LINESTRING (1.0000000000000000 2.0000000000000000, \
/// 3.0000000000000000 4.0000000000000000)");
/// ```
pub fn create_line_string(self) -> GResult<Geometry<'a>> {
pub fn create_line_string(self) -> GResult<Geometry> {
Geometry::create_line_string(self)
}

/// Creates a linear ring geometry.
pub fn create_linear_ring(self) -> GResult<Geometry<'a>> {
pub fn create_linear_ring(self) -> GResult<Geometry> {
Geometry::create_linear_ring(self)
}
}

unsafe impl<'a> Send for CoordSeq<'a> {}
unsafe impl<'a> Sync for CoordSeq<'a> {}
unsafe impl Send for CoordSeq {}
unsafe impl Sync for CoordSeq {}

impl<'a> Drop for CoordSeq<'a> {
impl Drop for CoordSeq {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
Expand All @@ -856,9 +856,9 @@ impl<'a> Drop for CoordSeq<'a> {
}
}

impl<'a> Clone for CoordSeq<'a> {
impl Clone for CoordSeq {
/// Also pass the context to the newly created `CoordSeq`.
fn clone(&self) -> CoordSeq<'a> {
fn clone(&self) -> CoordSeq {
let ptr = unsafe { GEOSCoordSeq_clone_r(self.get_raw_context(), self.as_raw()) };
if ptr.is_null() {
panic!("Couldn't clone CoordSeq...");
Expand All @@ -872,7 +872,7 @@ impl<'a> Clone for CoordSeq<'a> {
}
}

impl<'a> ContextInteractions<'a> for CoordSeq<'a> {
impl ContextInteractions for CoordSeq {
/// Set the context handle to the `CoordSeq`.
///
/// ```
Expand All @@ -883,7 +883,7 @@ impl<'a> ContextInteractions<'a> for CoordSeq<'a> {
/// let mut coord_seq = CoordSeq::new(2, CoordDimensions::TwoD).expect("failed to create CoordSeq");
/// coord_seq.set_context_handle(context_handle);
/// ```
fn set_context_handle(&mut self, context: ContextHandle<'a>) {
fn set_context_handle(&mut self, context: ContextHandle) {
self.context = Arc::new(context);
}

Expand All @@ -896,35 +896,35 @@ impl<'a> ContextInteractions<'a> for CoordSeq<'a> {
/// let context = coord_seq.get_context_handle();
/// context.set_notice_message_handler(Some(Box::new(|s| println!("new message: {}", s))));
/// ```
fn get_context_handle(&self) -> &ContextHandle<'a> {
fn get_context_handle(&self) -> &ContextHandle {
&self.context
}
}

impl<'a> AsRaw for CoordSeq<'a> {
impl AsRaw for CoordSeq {
type RawType = GEOSCoordSequence;

fn as_raw(&self) -> *const Self::RawType {
*self.ptr
}
}

impl<'a> AsRawMut for CoordSeq<'a> {
impl AsRawMut for CoordSeq {
type RawType = GEOSCoordSequence;

unsafe fn as_raw_mut_override(&self) -> *mut Self::RawType {
*self.ptr
}
}

impl<'a> ContextHandling for CoordSeq<'a> {
type Context = Arc<ContextHandle<'a>>;
impl ContextHandling for CoordSeq {
type Context = Arc<ContextHandle>;

fn get_raw_context(&self) -> GEOSContextHandle_t {
self.context.as_raw()
}

fn clone_context(&self) -> Arc<ContextHandle<'a>> {
fn clone_context(&self) -> Arc<ContextHandle> {
Arc::clone(&self.context)
}
}
Loading
Loading