Skip to content

Commit

Permalink
[#504] Introduce generic pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 12, 2024
1 parent e883eb0 commit 2b3c766
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions iceoryx2-bb/elementary/src/generic_pointer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::pointer_trait::PointerTrait;

/// Trait that allows to use typed pointers as generic arguments for structs.
pub trait GenericPointer {
/// The underlying pointer type.
type Type<T>: PointerTrait<T>;
}
1 change: 1 addition & 0 deletions iceoryx2-bb/elementary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod enum_gen;
pub mod alignment;
pub mod allocator;
pub mod bump_allocator;
pub mod generic_pointer;
pub mod lazy_singleton;
pub mod math;
pub mod owning_pointer;
Expand Down
7 changes: 7 additions & 0 deletions iceoryx2-bb/elementary/src/owning_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use std::alloc::Layout;
use std::alloc::{alloc, dealloc};

use crate::generic_pointer::GenericPointer;
use crate::pointer_trait::PointerTrait;

pub struct GenericOwningPointer;

/// Representation of a pointer which owns its memory.
#[repr(C)]
#[derive(Debug)]
Expand Down Expand Up @@ -63,3 +66,7 @@ impl<T> PointerTrait<T> for OwningPointer<T> {
self.ptr
}
}

impl GenericPointer for GenericOwningPointer {
type Type<T> = OwningPointer<T>;
}
7 changes: 7 additions & 0 deletions iceoryx2-bb/elementary/src/relocatable_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@
//! }
//! ```
use crate::generic_pointer::GenericPointer;
pub use crate::pointer_trait::PointerTrait;
use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicIsize;
use std::{marker::PhantomData, ptr::NonNull};

pub struct GenericRelocatablePointer;

/// A [`RelocatablePointer`] stores only the distance from its memory starting position to the
/// memory location it is pointing to. When the [`RelocatablePointer`] is now shared between
/// processes its virtual memory starting position changes but the distance to the object it is
Expand Down Expand Up @@ -138,3 +141,7 @@ impl<T> PointerTrait<T> for RelocatablePointer<T> {
self.as_ptr() as *mut T
}
}

impl GenericPointer for GenericRelocatablePointer {
type Type<T> = RelocatablePointer<T>;
}

0 comments on commit 2b3c766

Please sign in to comment.