Replies: 6 comments 8 replies
-
@lnicola If we make this change we should probably work out some conventions ahead of time:
|
Beta Was this translation helpful? Give feedback.
-
To help inform our approach, I experimented with converting master...metasim:gdal:prototype/foreign_types So far I'm really liking the consistency and the better handling of returning borrowed types (e.g. a ExampleThis: foreign_type! {
/// A OpenGIS Spatial Reference System definition.
#[derive(Debug)]
pub unsafe type SpatialRef {
type CType = libc::c_void;
fn drop = gdal_sys::OSRRelease;
fn clone = gdal_sys::OSRClone;
}
} Expands to this (we don't have to use the macro, but I think it's worth it): #[doc = " A OpenGIS Spatial Reference System definition."]
#[derive(Debug)]
#[repr(transparent)]
pub struct SpatialRef(::foreign_types::export::NonNull<libc::c_void>);
#[doc = "A borrowed reference to a [`SpatialRef`](struct.SpatialRef.html)."]
pub struct SpatialRefRef(::foreign_types::Opaque);
unsafe impl ::foreign_types::ForeignType for SpatialRef {
type CType = libc::c_void;
type Ref = SpatialRefRef;
#[inline]
unsafe fn from_ptr(ptr: *mut libc::c_void) -> SpatialRef {
SpatialRef(<::foreign_types::export::NonNull<_>>::new_unchecked(ptr))
}
#[inline]
fn as_ptr(&self) -> *mut libc::c_void { <::foreign_types::export::NonNull<_>>::as_ptr(self.0) }
}
unsafe impl ::foreign_types::ForeignTypeRef for SpatialRefRef { type CType = libc::c_void; }
impl ::foreign_types::export::Drop for SpatialRef {
#[inline]
fn drop(&mut self) { unsafe { (gdal_sys::OSRRelease)(::foreign_types::ForeignType::as_ptr(self)); } }
}
impl ::foreign_types::export::Deref for SpatialRef {
type Target = SpatialRefRef;
#[inline]
fn deref(&self) -> &SpatialRefRef { unsafe { ::foreign_types::ForeignTypeRef::from_ptr(::foreign_types::ForeignType::as_ptr(self)) } }
}
impl ::foreign_types::export::DerefMut for SpatialRef {
#[inline]
fn deref_mut(&mut self) -> &mut SpatialRefRef { unsafe { ::foreign_types::ForeignTypeRef::from_ptr_mut(::foreign_types::ForeignType::as_ptr(self)) } }
}
impl ::foreign_types::export::Borrow<SpatialRefRef> for SpatialRef {
#[inline]
fn borrow(&self) -> &SpatialRefRef { &**self }
}
impl ::foreign_types::export::BorrowMut<SpatialRefRef> for SpatialRef {
#[inline]
fn borrow_mut(&mut self) -> &mut SpatialRefRef { &mut **self }
}
impl ::foreign_types::export::AsRef<SpatialRefRef> for SpatialRef {
#[inline]
fn as_ref(&self) -> &SpatialRefRef { &**self }
}
impl ::foreign_types::export::AsMut<SpatialRefRef> for SpatialRef {
#[inline]
fn as_mut(&mut self) -> &mut SpatialRefRef { &mut **self }
}
impl ::foreign_types::export::Clone for SpatialRef {
#[inline]
fn clone(&self) -> SpatialRef {
unsafe {
let ptr = (gdal_sys::OSRClone)(::foreign_types::ForeignType::as_ptr(self));
::foreign_types::ForeignType::from_ptr(ptr)
}
}
}
impl ::foreign_types::export::ToOwned for SpatialRefRef {
type Owned = SpatialRef;
#[inline]
fn to_owned(&self) -> SpatialRef {
unsafe {
let ptr = (gdal_sys::OSRClone)(::foreign_types::ForeignTypeRef::as_ptr(self));
::foreign_types::ForeignType::from_ptr(ptr)
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Notes on
|
Beta Was this translation helpful? Give feedback.
-
Notes on Migration
|
Beta Was this translation helpful? Give feedback.
-
Posting as top-level because I didn't look in close detail to your changes and other questions, but I think:
So, while I don't particularly love |
Beta Was this translation helpful? Give feedback.
-
A PR has been submitted to provide a more concrete foundation to collaboratively further work this issue. |
Beta Was this translation helpful? Give feedback.
-
All the C getters (
c_dataset
and friends) areunsafe
, but this is totally unidiomatic, cf. https://doc.rust-lang.org/std/os/fd/trait.IntoRawFd.html#tymethod.into_raw_fd or pointer arithmetic. Rust considers it safe to manipulate pointers and other handles, while dereferencing or turning them back into something unsable is unsafe.I think we should follow the same pattern.
Update
by @metasim
The scope of this topic has expanded beyond just the C handle getters into how we might be more principled, consistent and idiomatic.
Related
c_dataset
and friends) should be safe #168Beta Was this translation helpful? Give feedback.
All reactions