Constant CUSTOM_RB_CODE

Source
pub const CUSTOM_RB_CODE: &str = "use std::os::raw::c_void;\n\nunsafe impl Sync for AeronSpscRb {}\nunsafe impl Sync for AeronMpscRb {}\nunsafe impl Send for AeronSpscRb {}\nunsafe impl Send for AeronMpscRb {}\nunsafe impl Send for AeronBroadcastTransmitter {}\nunsafe impl Send for AeronBroadcastReceiver {}\n\npub const AERON_BROADCAST_BUFFER_TRAILER_LENGTH: usize = size_of::<aeron_broadcast_descriptor_t>();\n\nmacro_rules! impl_buffer_methods {\n    ($t:ty) => {\n        impl $t {\n            #[inline]\n            pub fn buffer_mut(&self) -> &mut [u8] {\n                debug_assert!(!self.buffer.is_null());\n                unsafe { std::slice::from_raw_parts_mut(self.buffer, self.capacity) }\n            }\n\n            #[inline]\n            pub fn buffer_at_mut(&self, idx: usize, len: usize) -> &mut [u8] {\n                debug_assert!(idx + len < self.capacity);\n                debug_assert!(!self.buffer.is_null());\n                unsafe { std::slice::from_raw_parts_mut(self.buffer.add(idx), len) }\n            }\n        }\n    };\n}\n\nimpl_buffer_methods!(AeronBroadcastTransmitter);\nimpl_buffer_methods!(AeronBroadcastReceiver);\nimpl_buffer_methods!(AeronSpscRb);\nimpl_buffer_methods!(AeronMpscRb);\n\nmacro_rules! impl_from_vec_and_new_with_capacity {\n    ($t:ident, $slot:ident, $descriptor:ty) => {\n        pub struct $slot<\'a> {\n            pub idx: i32,\n            pub length: usize,\n            commited: bool,\n            rb: &\'a $t,\n        }\n\n        impl<\'a> $slot<\'a> {\n            pub fn commit(mut self) -> Result<i32, AeronCError> {\n                self.commited = true;\n                self.rb.commit(self.idx)\n            }\n            pub fn abort(mut self) -> Result<i32, AeronCError> {\n                self.commited = true;\n                self.rb.abort(self.idx)\n            }\n            pub fn buffer_mut(&self) -> &mut [u8] {\n                self.rb.buffer_at_mut(self.idx as usize, self.length)\n            }\n        }\n\n        impl<\'a> std::ops::Deref for $slot<\'a> {\n            type Target = [u8];\n\n            fn deref(&self) -> &Self::Target {\n                self.buffer_mut()\n            }\n        }\n\n        impl<\'a> std::ops::DerefMut for $slot<\'a> {\n            fn deref_mut(&mut self) -> &mut Self::Target {\n                self.buffer_mut()\n            }\n        }\n\n        impl<\'a> Drop for $slot<\'a> {\n            fn drop(&mut self) {\n                if !self.commited {\n                    let _ = self.rb.commit(self.idx);\n                }\n            }\n        }\n\n        impl $t {\n            pub fn try_claim_slice<\'a>(\n                &\'a self,\n                msg_type_id: i32,\n                length: usize,\n            ) -> Result<$slot<\'a>, AeronCError> {\n                let idx = self.try_claim(msg_type_id, length);\n                if idx <= 0 {\n                    Err(AeronCError::from_code(idx))\n                } else {\n                    Ok($slot {\n                        idx,\n                        length,\n                        commited: false,\n                        rb: self,\n                    })\n                }\n            }\n\n            pub fn from_slice(buffer: &mut [u8], max_msg_size: usize) -> Result<Self, AeronCError> {\n                assert!(!buffer.is_empty());\n                assert!(buffer.len().is_power_of_two());\n                Self::new(\n                    buffer.as_mut_ptr(),\n                    &<$descriptor>::default(),\n                    buffer.len(),\n                    max_msg_size,\n                )\n            }\n\n            pub fn new_with_capacity(\n                capacity: usize,\n                max_msg_size: usize,\n            ) -> Result<Self, AeronCError> {\n                assert!(capacity.is_power_of_two());\n                Self::from_slice(vec![0u8; capacity].leak(), max_msg_size)\n            }\n        }\n    };\n}\n\nimpl_from_vec_and_new_with_capacity!(AeronSpscRb, AeronSpscRbSlot, AeronRbDescriptor);\nimpl_from_vec_and_new_with_capacity!(AeronMpscRb, AeronMpscRbSlot, AeronRbDescriptor);\n\nimpl AeronBroadcastTransmitter {\n    pub fn from_slice(buffer: &mut [u8], max_msg_size: usize) -> Result<Self, AeronCError> {\n        assert!(!buffer.is_empty());\n        assert!((buffer.len() - AERON_BROADCAST_BUFFER_TRAILER_LENGTH).is_power_of_two());\n\n        let ptr = buffer.as_mut_ptr();\n        let broadcast = Self::new(\n            ptr,\n            &AeronBroadcastDescriptor::default(),\n            buffer.len(),\n            max_msg_size,\n        )?;\n        broadcast.init(ptr as *mut _, buffer.len())?;\n        Ok(broadcast)\n    }\n\n    pub fn transmit_msg(&self, msg_type_id: i32, msg: &[u8]) -> Result<i32, AeronCError> {\n        debug_assert!(msg.len() > 0);\n        debug_assert!(msg_type_id > 0);\n        self.transmit(msg_type_id, msg.as_ptr() as *const _, msg.len())\n    }\n}\n\nimpl AeronBroadcastReceiver {\n    pub fn from_slice(buffer: &mut [u8]) -> Result<Self, AeronCError> {\n        assert!(!buffer.is_empty());\n        let capacity = buffer.len();\n        assert!((capacity - AERON_BROADCAST_BUFFER_TRAILER_LENGTH).is_power_of_two());\n\n        let ptr = buffer.as_mut_ptr();\n        let broadcast = Self::new(\n            [0u8; 4096],\n            ptr,\n            &AeronBroadcastDescriptor::default(),\n            capacity,\n            capacity - 1,\n            0,\n            0,\n            0,\n            0,\n        )?;\n        broadcast.init(ptr as *mut _, capacity)?;\n        Ok(broadcast)\n    }\n}\n\nimpl AeronSpscRb {\n    pub fn read_msgs<T: AeronRingBufferHandlerCallback>(\n        &self,\n        handler: &Handler<AeronRingBufferHandlerWrapper<T>>,\n        message_count_limit: usize,\n    ) -> usize {\n        self.read(Some(handler), message_count_limit)\n    }\n\n    pub fn controlled_read_msgs<T: AeronRingBufferControlledHandlerCallback>(\n        &self,\n        handler: &Handler<AeronRingBufferControlledHandlerWrapper<T>>,\n        message_count_limit: usize,\n    ) -> usize {\n        self.controlled_read(Some(handler), message_count_limit)\n    }\n}\n\nimpl AeronMpscRb {\n    pub fn read_msgs<T: AeronRingBufferHandlerCallback>(\n        &self,\n        handler: &Handler<AeronRingBufferHandlerWrapper<T>>,\n        message_count_limit: usize,\n    ) -> usize {\n        self.read(Some(handler), message_count_limit)\n    }\n\n    pub fn controlled_read_msgs<T: AeronRingBufferControlledHandlerCallback>(\n        &self,\n        handler: &Handler<AeronRingBufferControlledHandlerWrapper<T>>,\n        message_count_limit: usize,\n    ) -> usize {\n        self.controlled_read(Some(handler), message_count_limit)\n    }\n}\n\npub struct AeronRingBufferHandlerWrapper<T: AeronRingBufferHandlerCallback> {\n    pub handler: T,\n}\n\nimpl<T: AeronRingBufferHandlerCallback> std::ops::Deref for AeronRingBufferHandlerWrapper<T> {\n    type Target = T;\n\n    fn deref(&self) -> &Self::Target {\n        &self.handler\n    }\n}\n\nimpl<T: AeronRingBufferHandlerCallback> std::ops::DerefMut for AeronRingBufferHandlerWrapper<T> {\n    fn deref_mut(&mut self) -> &mut Self::Target {\n        &mut self.handler\n    }\n}\n\nimpl<T: AeronRingBufferHandlerCallback> AeronRingBufferHandlerWrapper<T> {\n    pub fn new(handler: T) -> Handler<Self> {\n        Handler::leak(Self { handler })\n    }\n}\nimpl<T: AeronRingBufferHandlerCallback> AeronRbHandlerCallback\n    for AeronRingBufferHandlerWrapper<T>\n{\n    fn handle_aeron_rb_handler(&mut self, msg_id: i32, buffer: *const c_void, length: usize) -> () {\n        let buffer = unsafe { std::slice::from_raw_parts(buffer as *const u8, length) };\n        self.handler.handle_aeron_rb_handler(msg_id, buffer);\n    }\n}\n\npub trait AeronRingBufferHandlerCallback {\n    fn handle_aeron_rb_handler(&mut self, msg_type_id: i32, buffer: &[u8]) -> ();\n}\n\npub struct AeronRingBufferControlledHandlerWrapper<T: AeronRingBufferControlledHandlerCallback> {\n    handler: T,\n}\n\nimpl<T: AeronRingBufferControlledHandlerCallback> AeronRingBufferControlledHandlerWrapper<T> {\n    pub fn new(handler: T) -> Handler<Self> {\n        Handler::leak(Self { handler })\n    }\n}\nimpl<T: AeronRingBufferControlledHandlerCallback> AeronRbControlledHandlerCallback\n    for AeronRingBufferControlledHandlerWrapper<T>\n{\n    fn handle_aeron_rb_controlled_handler(\n        &mut self,\n        msg_id: i32,\n        buffer: *const c_void,\n        length: usize,\n    ) -> aeron_rb_read_action_t {\n        let buffer = unsafe { std::slice::from_raw_parts(buffer as *const u8, length) };\n        self.handler\n            .handle_aeron_controlled_rb_handler(msg_id, buffer)\n    }\n}\n\npub trait AeronRingBufferControlledHandlerCallback {\n    fn handle_aeron_controlled_rb_handler(\n        &mut self,\n        msg_type_id: i32,\n        buffer: &[u8],\n    ) -> aeron_rb_read_action_t;\n}\n";