Skip to content

Commit

Permalink
Add lifetimes to nt types and add nt macro
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueZeeKing committed Aug 4, 2024
1 parent f47407d commit b7001e6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
49 changes: 26 additions & 23 deletions crates/nt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[allow(warnings)]
pub mod bindings;
pub mod macros;
pub mod options;
pub mod payloads;

use std::{ffi::CString, marker::PhantomData};
use std::{ffi::CString, marker::PhantomData, sync::LazyLock};

use bindings::*;
use options::PubSubOptions;
Expand All @@ -13,13 +14,9 @@ pub struct Instance {
handle: NT_Inst,
}

impl Default for Instance {
fn default() -> Self {
Self {
handle: unsafe { NT_GetDefaultInstance() },
}
}
}
static DEFAULT_INSTANCE: LazyLock<Instance> = LazyLock::new(|| Instance {
handle: unsafe { NT_GetDefaultInstance() },
});

impl Instance {
pub fn new() -> Self {
Expand All @@ -28,7 +25,11 @@ impl Instance {
}
}

pub fn topic(&self, name: &str) -> Topic {
pub fn default_instance() -> &'static Self {
&DEFAULT_INSTANCE
}

pub fn topic(&self, name: &str) -> Topic<'_> {
Topic {
handle: unsafe {
NT_GetTopic(
Expand All @@ -37,6 +38,7 @@ impl Instance {
name.len(),
)
},
phantom: PhantomData,
}
}

Expand All @@ -58,12 +60,13 @@ impl Instance {
}
}

pub struct Topic {
pub struct Topic<'a> {
handle: NT_Topic,
phantom: PhantomData<&'a ()>,
}

impl Topic {
pub fn publish<T: Payload>(&self, options: PubSubOptions) -> Publisher<T> {
impl<'a> Topic<'a> {
pub fn publish<T: Payload>(&self, options: PubSubOptions) -> Publisher<'a, T> {
Publisher {
handle: unsafe {
NT_Publish(
Expand All @@ -81,7 +84,7 @@ impl Topic {
&self,
options: PubSubOptions,
type_str: &str,
) -> Publisher<T> {
) -> Publisher<'a, T> {
Publisher {
handle: unsafe {
NT_Publish(
Expand All @@ -95,7 +98,7 @@ impl Topic {
}
}

pub fn subscribe<T: Payload>(&self, options: PubSubOptions) -> Subscriber<T> {
pub fn subscribe<T: Payload>(&self, options: PubSubOptions) -> Subscriber<'a, T> {
Subscriber {
handle: unsafe {
NT_Subscribe(
Expand All @@ -113,7 +116,7 @@ impl Topic {
&self,
options: PubSubOptions,
type_str: &str,
) -> Subscriber<T> {
) -> Subscriber<'a, T> {
Subscriber {
handle: unsafe {
NT_Subscribe(
Expand All @@ -128,23 +131,23 @@ impl Topic {
}
}

pub struct Publisher<T> {
pub struct Publisher<'a, T> {
handle: NT_Publisher,
payload: PhantomData<T>,
payload: PhantomData<&'a T>,
}

impl<T: Payload> Publisher<T> {
impl<'a, T: Payload> Publisher<'a, T> {
pub fn set(&self, value: T) {
value.to_entry(self.handle, unsafe { NT_Now() });
}
}

pub struct Subscriber<T> {
pub struct Subscriber<'a, T> {
handle: NT_Subscriber,
payload: PhantomData<T>,
payload: PhantomData<&'a T>,
}

impl<T: Payload> Subscriber<T> {
impl<'a, T: Payload> Subscriber<'a, T> {
pub fn get_with_default(&self, default: T) -> T {
T::from_entry(self.handle, default)
}
Expand All @@ -157,13 +160,13 @@ impl<T: Payload> Subscriber<T> {
}
}

impl<T> Drop for Subscriber<T> {
impl<'a, T> Drop for Subscriber<'a, T> {
fn drop(&mut self) {
unsafe { NT_Unsubscribe(self.handle) }
}
}

impl<T> Drop for Publisher<T> {
impl<'a, T> Drop for Publisher<'a, T> {
fn drop(&mut self) {
unsafe { NT_Unpublish(self.handle) }
}
Expand Down
34 changes: 34 additions & 0 deletions crates/nt/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::sync::OnceLock;

use crate::{payloads::Payload, Instance, NT_Now, NT_Publisher};

#[macro_export]
macro_rules! nt {
($topic_name:literal, $value:expr) => {{
static PUBLISHER: ::std::sync::OnceLock<$crate::bindings::NT_Publisher> =
::std::sync::OnceLock::new();

$crate::macros::_internal_set(&PUBLISHER, $topic_name, $value);
}};
}

pub fn _internal_set<T: Payload>(
handle: &'static OnceLock<NT_Publisher>,
topic_name: &str,
value: T,
) {
value.to_entry(
*handle.get_or_init(|| {
let publisher = Instance::default_instance()
.topic(topic_name)
.publish::<T>(Default::default());

let handle = publisher.handle;

std::mem::forget(publisher);

handle
}),
unsafe { NT_Now() },
);
}
6 changes: 4 additions & 2 deletions crates/nt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{thread, time::Duration};
use nt::{Instance, Publisher, Subscriber};

fn main() {
let server = Instance::default();
let server = Instance::default_instance();
server.start_server("nt.json");
if server.is_starting() {
let mut started = false;
Expand All @@ -22,7 +22,9 @@ fn main() {
}
}

let publisher: Publisher<String> = server.topic("/test/value").publish(Default::default());
nt::nt!("/test/publish", 123);

let publisher: Publisher<String> = server.topic("/test/other").publish(Default::default());

publisher.set("Test".to_string());

Expand Down

0 comments on commit b7001e6

Please sign in to comment.