Skip to content

Commit

Permalink
sourceInfo updated
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jun 25, 2024
1 parent 55ab839 commit 3032e7e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
34 changes: 15 additions & 19 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ use std::str::from_utf8;
use std::str::FromStr;

use crate::errors;
use crate::transmute::Inplace;
use crate::transmute::TransmuteCopy;
use crate::transmute::TransmuteFromHandle;
use crate::transmute::TransmuteIntoHandle;
use crate::transmute::TransmuteRef;
use crate::transmute::TransmuteUninitPtr;
use crate::transmute2::CTypeRef;
use crate::transmute2::IntoCType;
use crate::transmute2::LoanedCTypeRef;
Expand Down Expand Up @@ -168,7 +164,7 @@ pub extern "C" fn z_sample_attachment(this: &z_loaned_sample_t) -> *const z_loan
/// Returns the sample source_info.
#[no_mangle]
pub extern "C" fn z_sample_source_info(this: &z_loaned_sample_t) -> &z_loaned_source_info_t {
this.as_rust_type_ref().source_info().transmute_handle()
this.as_rust_type_ref().source_info().as_loaned_ctype_ref()
}

/// Constructs an owned shallow copy of the sample (i.e. all modficiations applied to the copy, might be visible in the original) in provided uninitilized memory location.
Expand Down Expand Up @@ -589,32 +585,32 @@ pub extern "C" fn z_entity_global_id_eid(this: &z_entity_global_id_t) -> u32 {
this.as_rust_type_ref().eid()
}
pub use crate::opaque_types::z_loaned_source_info_t;
decl_transmute_handle!(SourceInfo, z_loaned_source_info_t);
pub use crate::opaque_types::z_owned_source_info_t;
decl_transmute_owned!(SourceInfo, z_owned_source_info_t);

validate_equivalence!(z_owned_source_info_t, z_loaned_source_info_t);
decl_c_type!(
owned(z_owned_source_info_t, SourceInfo),
loaned(z_loaned_source_info_t, SourceInfo)
);

/// Create source info
#[no_mangle]
pub extern "C" fn z_source_info_new(
this: *mut MaybeUninit<z_owned_source_info_t>,
this: &mut MaybeUninit<z_owned_source_info_t>,
source_id: &z_entity_global_id_t,
source_sn: u64,
) -> errors::z_error_t {
let this = this.transmute_uninit_ptr();
let this = this.as_rust_type_mut_uninit();
let source_info = SourceInfo {
source_id: Some(*source_id.as_rust_type_ref()),
source_sn: Some(source_sn),
};
Inplace::init(this, source_info);
this.write(source_info);
errors::Z_OK
}

/// Returns the source_id of the source info.
#[no_mangle]
pub extern "C" fn z_source_info_id(this: &z_loaned_source_info_t) -> z_entity_global_id_t {
match this.transmute_ref().source_id {
match this.as_rust_type_ref().source_id {
Some(source_id) => source_id,
None => EntityGlobalId::default(),
}
Expand All @@ -624,29 +620,29 @@ pub extern "C" fn z_source_info_id(this: &z_loaned_source_info_t) -> z_entity_gl
/// Returns the source_sn of the source info.
#[no_mangle]
pub extern "C" fn z_source_info_sn(this: &z_loaned_source_info_t) -> u64 {
this.transmute_ref().source_sn.unwrap_or(0)
this.as_rust_type_ref().source_sn.unwrap_or(0)
}

/// Returns ``true`` if source info is valid, ``false`` if it is in gravestone state.
#[no_mangle]
pub extern "C" fn z_source_info_check(this: &z_owned_source_info_t) -> bool {
this.transmute_ref().source_id.is_some() || this.transmute_ref().source_sn.is_some()
this.as_rust_type_ref().source_id.is_some() || this.as_rust_type_ref().source_sn.is_some()
}

/// Borrows source info.
#[no_mangle]
pub extern "C" fn z_source_info_loan(this: &z_owned_source_info_t) -> &z_loaned_source_info_t {
this.transmute_ref().transmute_handle()
this.as_rust_type_ref().as_loaned_ctype_ref()
}

/// Frees the memory and invalidates the source info, resetting it to a gravestone state.
#[no_mangle]
pub extern "C" fn z_source_info_drop(this: &mut z_owned_source_info_t) {
Inplace::drop(this.transmute_mut());
*this.as_rust_type_mut() = SourceInfo::default();
}

/// Constructs source info in its gravestone state.
#[no_mangle]
pub extern "C" fn z_source_info_null(this: *mut MaybeUninit<z_owned_source_info_t>) {
Inplace::empty(this.transmute_uninit_ptr());
pub extern "C" fn z_source_info_null(this: &mut MaybeUninit<z_owned_source_info_t>) {
this.as_rust_type_mut_uninit().write(SourceInfo::default());
}
2 changes: 1 addition & 1 deletion src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub unsafe extern "C" fn z_get(
get = get.encoding(encoding);
}
if let Some(source_info) = unsafe { options.source_info.as_mut() } {
let source_info = source_info.transmute_mut().extract();
let source_info = std::mem::take(source_info.as_rust_type_mut());
get = get.source_info(source_info);
}
if let Some(attachment) = unsafe { options.attachment.as_mut() } {
Expand Down
2 changes: 1 addition & 1 deletion src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub unsafe extern "C" fn z_publisher_put(
put = put.encoding(encoding);
};
if let Some(source_info) = unsafe { options.source_info.as_mut() } {
let source_info = source_info.transmute_mut().extract();
let source_info = std::mem::take(source_info.as_rust_type_mut());
put = put.source_info(source_info);
};
if let Some(attachment) = unsafe { options.attachment.as_mut() } {
Expand Down
3 changes: 2 additions & 1 deletion src/put.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::mem::take;
use std::ptr::null_mut;

//
Expand Down Expand Up @@ -96,7 +97,7 @@ pub extern "C" fn z_put(
put = put.encoding(encoding);
};
if let Some(source_info) = unsafe { options.source_info.as_mut() } {
let source_info = source_info.transmute_mut().extract();
let source_info = std::mem::take(source_info.as_rust_type_mut());
put = put.source_info(source_info);
};
if let Some(attachment) = unsafe { options.attachment.as_mut() } {
Expand Down
4 changes: 2 additions & 2 deletions src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub extern "C" fn z_query_reply(
reply = reply.encoding(encoding);
};
if let Some(source_info) = unsafe { options.source_info.as_mut() } {
let source_info = source_info.transmute_mut().extract();
let source_info = std::mem::take(source_info.as_rust_type_mut());
reply = reply.source_info(source_info);
};
if let Some(attachment) = unsafe { options.attachment.as_mut() } {
Expand Down Expand Up @@ -390,7 +390,7 @@ pub unsafe extern "C" fn z_query_reply_del(
let mut reply = query.reply_del(key_expr);
if let Some(options) = options {
if let Some(source_info) = unsafe { options.source_info.as_mut() } {
let source_info = source_info.transmute_mut().extract();
let source_info = std::mem::take(source_info.as_rust_type_mut());
reply = reply.source_info(source_info);
};
if let Some(attachment) = unsafe { options.attachment.as_mut() } {
Expand Down
2 changes: 1 addition & 1 deletion src/querying_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub unsafe extern "C" fn ze_querying_subscriber_get(
get = get.encoding(encoding);
}
if let Some(source_info) = unsafe { options.source_info.as_mut() } {
let source_info = source_info.transmute_mut().extract();
let source_info = std::mem::take(source_info.as_rust_type_mut());
get = get.source_info(source_info);
}
if let Some(attachment) = unsafe { options.attachment.as_mut() } {
Expand Down

0 comments on commit 3032e7e

Please sign in to comment.