tail_core 0.1.0

Core library for the Tail operating system
Documentation
// Copyright 2025, TAIL OS. All Rights Reserved.
//
// You must obtain a written license from and pay applicable license fees to TAIL OS
// before you may reproduce, modify, or distribute this software, or any work that
// includes all or part of this software.
//
// Free development licenses are available for evaluation, research, and non-commercial
// purposes, which may include access to the source code under these terms. Redistribution
// or commercial use without a license is strictly prohibited.
//
// This file may contain contributions from others. Please review this entire file for
// other proprietary rights or license notices, as well as the TAIL OS License Guide at
// https://tail-os.com/license-guide/ for more information.
//
// For licensing inquiries, visit https://tail-os.com or email license@tail-os.com.


use crate::syscall;
use crate::tail::TOPIC_NAME_MAX_LENGTH;

/// Represents a topic for sending and receiving messages.
///
/// # Deprecated
///
/// This type is obsolete and will be removed in a future version.
/// The topic-based IPC mechanism is being phased out.
#[deprecated(note = "Publisher is obsolete. Use alternative IPC mechanisms.")]
pub struct Publisher<T: ?Sized> {
    topic_name: [u8; TOPIC_NAME_MAX_LENGTH],
    topic_name_len: usize,
    phantom: core::marker::PhantomData<T>,
}

impl<T: ?Sized> Publisher<T> {
    /// Creates a new publisher for the given topic name.
    ///
    /// # Panics
    ///
    /// Panics if the topic name length exceeds `TOPIC_NAME_MAX_LENGTH` or if
    /// the topic name is not valid UTF-8.
    ///
    /// # Deprecated
    ///
    /// This method is obsolete and will be removed in a future version.
    #[deprecated(note = "Publisher::new is obsolete. Use alternative IPC mechanisms.")]
    pub fn new(topic_name: &str) -> Publisher<T> {
        if topic_name.len() > TOPIC_NAME_MAX_LENGTH {
            panic!("Topic name length {} exceeds maximum length {}", topic_name.len(), TOPIC_NAME_MAX_LENGTH);
        }

        let mut publisher = Publisher {
            topic_name: [0; TOPIC_NAME_MAX_LENGTH],
            topic_name_len: topic_name.len(),
            phantom: core::marker::PhantomData,
        };

        // Safe: we've already checked the length
        publisher.topic_name[..topic_name.len()].copy_from_slice(topic_name.as_bytes());
        
        syscall::syscall_create_topic_old::<T>(topic_name);
        publisher
    }

    /// Gets the topic name as a string slice.
    ///
    /// # Panics
    ///
    /// Panics if the stored topic name is not valid UTF-8 (should not happen
    /// if created through `new`).
    pub fn topic_name(&self) -> &str {
        core::str::from_utf8(&self.topic_name[..self.topic_name_len])
            .expect("Topic name should always be valid UTF-8")
    }

    /// Publishes a message to the topic.
    ///
    /// # Arguments
    ///
    /// * `message` - The message to publish
    /// * `message_size` - The size of the message in bytes
    ///
    /// # Deprecated
    ///
    /// This method is obsolete and will be removed in a future version.
    #[deprecated(note = "Publisher::publish is obsolete. Use alternative IPC mechanisms.")]
    pub fn publish(&self, message: &T, message_size: usize) {
        syscall::syscall_publish_to_topic(self.topic_name(), message, message_size);
    }
}

impl<T> Publisher<T> {
    /// Publishes a message to the topic.
    ///
    /// This is a convenience method for sized types that automatically
    /// calculates the message size.
    ///
    /// # Arguments
    ///
    /// * `message` - The message to publish
    ///
    /// # Deprecated
    ///
    /// This method is obsolete and will be removed in a future version.
    #[deprecated(note = "Publisher::publish_sized is obsolete. Use alternative IPC mechanisms.")]
    pub fn publish_sized(&self, message: &T) {
        let message_size = core::mem::size_of::<T>();
        syscall::syscall_publish_to_topic(self.topic_name(), message, message_size);
    }
}