Skip to content

Commit

Permalink
seal object reference trait.
Browse files Browse the repository at this point in the history
This change adds the private ObjectReferencePrivate trait which is used
to seal the ObjectReference trait and prevent outside implementations.

Refs: #36
  • Loading branch information
JamesMc86 committed Nov 26, 2024
1 parent eb70091 commit 6154664
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion hdf5/src/hl/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ pub use legacy::ObjectReference1;
#[cfg(feature = "1.12.1")]
pub use standard::ObjectReference2;

pub trait ObjectReference: Sized + H5Type {
mod private {
pub trait ObjectReferencePrivate {}
}

/// The trait for all object references. This provides a common interface
/// over the legacy and standard reference types.
///
/// This trait is sealed and cannot be implemented for types outside `hdf5::hl`.
pub trait ObjectReference: Sized + H5Type + private::ObjectReferencePrivate {
const REF_TYPE: H5R_type_t;
fn ptr(&self) -> *const c_void;

Expand Down
5 changes: 4 additions & 1 deletion hdf5/src/hl/references/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use hdf5_sys::h5r::H5R_OBJECT as H5R_OBJECT1;
#[cfg(feature = "1.12.0")]
use hdf5_sys::h5r::H5R_OBJECT1;

use crate::{Location, ObjectReference};
use crate::{Location};
use super::{ObjectReference, private::ObjectReferencePrivate};

#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
Expand All @@ -28,6 +29,8 @@ unsafe impl H5Type for ObjectReference1 {
}
}

impl ObjectReferencePrivate for ObjectReference1 {}

impl ObjectReference for ObjectReference1 {
const REF_TYPE: hdf5_sys::h5r::H5R_type_t = H5R_OBJECT1;

Expand Down
4 changes: 3 additions & 1 deletion hdf5/src/hl/references/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hdf5_sys::h5o::H5O_type_t;
use hdf5_sys::h5r::H5R_type_t::H5R_OBJECT2;
use hdf5_sys::h5r::{H5R_ref_t, H5Rcreate_object, H5Rdestroy, H5Rget_obj_type3, H5Ropen_object};

use super::ObjectReference;
use super::{ObjectReference, private::ObjectReferencePrivate};
use crate::internal_prelude::*;
use crate::Location;

Expand Down Expand Up @@ -43,6 +43,8 @@ impl Drop for StdReference {
#[derive(Debug)]
pub struct ObjectReference2(StdReference);

impl ObjectReferencePrivate for ObjectReference2 {}

impl ObjectReference for ObjectReference2 {
const REF_TYPE: hdf5_sys::h5r::H5R_type_t = H5R_OBJECT2;

Expand Down

0 comments on commit 6154664

Please sign in to comment.