Skip to content

Commit

Permalink
[eclipse-iceoryx#195] Move TypeDetails into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Apr 25, 2024
1 parent 790a49a commit 67ce663
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 78 deletions.
2 changes: 1 addition & 1 deletion iceoryx2/src/service/builder/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use iceoryx2_cal::dynamic_storage::DynamicStorageCreateError;
use iceoryx2_cal::serialize::Serialize;
use iceoryx2_cal::static_storage::StaticStorageLocked;

use self::static_config::publish_subscribe::{TypeDetails, TypeVariantBuilder};
use self::type_details::{TypeDetails, TypeVariantBuilder};

use super::ServiceState;

Expand Down
2 changes: 2 additions & 0 deletions iceoryx2/src/service/static_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod event;
/// based service.
pub mod publish_subscribe;

pub mod type_details;

use crate::service::messaging_pattern::MessagingPattern;
use iceoryx2_bb_log::fatal_panic;
use iceoryx2_cal::hash::Hash;
Expand Down
77 changes: 1 addition & 76 deletions iceoryx2/src/service/static_config/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
//! # }
//! ```
use std::{alloc::Layout, marker::PhantomData};

use super::type_details::{TypeDetails, TypeVariant};
use crate::config;
use iceoryx2_bb_elementary::math::align;
use serde::{Deserialize, Serialize};

/// The static configuration of an
Expand All @@ -55,79 +53,6 @@ pub struct StaticConfig {
pub(crate) type_details: TypeDetails,
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum TypeVariant {
FixedSize,
Dynamic,
}

pub(crate) struct TypeVariantBuilder<T: ?Sized> {
_data: PhantomData<T>,
}

impl<T> TypeVariantBuilder<T> {
pub(crate) fn new() -> TypeVariant {
TypeVariant::FixedSize
}
}

impl<T> TypeVariantBuilder<[T]> {
pub(crate) fn new() -> TypeVariant {
TypeVariant::Dynamic
}
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct TypeDetails {
pub variant: TypeVariant,
pub header_type_name: String,
pub header_size: usize,
pub header_alignment: usize,
pub message_type_name: String,
pub message_size: usize,
pub message_alignment: usize,
}

impl TypeDetails {
pub fn from<MessageType, Header>(variant: TypeVariant) -> Self {
Self {
variant,
header_type_name: core::any::type_name::<Header>().to_string(),
header_size: core::mem::size_of::<Header>(),
header_alignment: core::mem::align_of::<Header>(),
message_type_name: core::any::type_name::<MessageType>().to_string(),
message_size: core::mem::size_of::<MessageType>(),
message_alignment: core::mem::align_of::<MessageType>(),
}
}

pub fn sample_layout(&self, number_of_elements: usize) -> Layout {
let aligned_header_size = align(self.header_size, self.message_alignment);
unsafe {
Layout::from_size_align_unchecked(
align(
aligned_header_size + self.message_size * number_of_elements,
self.header_alignment,
),
self.header_alignment,
)
}
}

pub fn message_layout(&self, number_of_elements: usize) -> Layout {
unsafe {
Layout::from_size_align_unchecked(
self.message_size * number_of_elements,
self.message_alignment,
)
}
}

pub fn is_compatible(&self, rhs: &Self) -> bool {
self == rhs
}
}

impl StaticConfig {
pub(crate) fn new(config: &config::Config) -> Self {
Self {
Expand Down
89 changes: 89 additions & 0 deletions iceoryx2/src/service/static_config/type_details.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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 std::{alloc::Layout, marker::PhantomData};

use iceoryx2_bb_elementary::math::align;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum TypeVariant {
FixedSize,
Dynamic,
}

pub(crate) struct TypeVariantBuilder<T: ?Sized> {
_data: PhantomData<T>,
}

impl<T> TypeVariantBuilder<T> {
pub(crate) fn new() -> TypeVariant {
TypeVariant::FixedSize
}
}

impl<T> TypeVariantBuilder<[T]> {
pub(crate) fn new() -> TypeVariant {
TypeVariant::Dynamic
}
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct TypeDetails {
pub variant: TypeVariant,
pub header_type_name: String,
pub header_size: usize,
pub header_alignment: usize,
pub message_type_name: String,
pub message_size: usize,
pub message_alignment: usize,
}

impl TypeDetails {
pub fn from<MessageType, Header>(variant: TypeVariant) -> Self {
Self {
variant,
header_type_name: core::any::type_name::<Header>().to_string(),
header_size: core::mem::size_of::<Header>(),
header_alignment: core::mem::align_of::<Header>(),
message_type_name: core::any::type_name::<MessageType>().to_string(),
message_size: core::mem::size_of::<MessageType>(),
message_alignment: core::mem::align_of::<MessageType>(),
}
}

pub fn sample_layout(&self, number_of_elements: usize) -> Layout {
let aligned_header_size = align(self.header_size, self.message_alignment);
unsafe {
Layout::from_size_align_unchecked(
align(
aligned_header_size + self.message_size * number_of_elements,
self.header_alignment,
),
self.header_alignment,
)
}
}

pub fn message_layout(&self, number_of_elements: usize) -> Layout {
unsafe {
Layout::from_size_align_unchecked(
self.message_size * number_of_elements,
self.message_alignment,
)
}
}

pub fn is_compatible(&self, rhs: &Self) -> bool {
self == rhs
}
}
2 changes: 1 addition & 1 deletion iceoryx2/tests/service_publish_subscribe_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod service_publish_subscribe {
use iceoryx2::service::builder::publish_subscribe::PublishSubscribeCreateError;
use iceoryx2::service::builder::publish_subscribe::PublishSubscribeOpenError;
use iceoryx2::service::port_factory::publisher::UnableToDeliverStrategy;
use iceoryx2::service::static_config::publish_subscribe::TypeVariant;
use iceoryx2::service::static_config::type_details::TypeVariant;
use iceoryx2::service::static_config::StaticConfig;
use iceoryx2::service::Service;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
Expand Down

0 comments on commit 67ce663

Please sign in to comment.