From db2496faa212fabcd09c0f5aaaa58bb5935dfe72 Mon Sep 17 00:00:00 2001 From: Ammar Abou Zor Date: Thu, 12 Sep 2024 15:02:14 +0200 Subject: [PATCH] Rust native support for 'ByteSource' async trait * Use rust native support for async traits for the trait 'ByteSource' instead of the crate `async-traits` avoiding the extra heap allocation on the return value. * Warning about async in public traits is suppressed since we are using the trait in our own codebase only. --- application/apps/indexer/Cargo.lock | 12 ------------ application/apps/indexer/sources/Cargo.toml | 1 - .../apps/indexer/sources/src/binary/pcap/legacy.rs | 2 -- .../apps/indexer/sources/src/binary/pcap/ng.rs | 2 -- application/apps/indexer/sources/src/binary/raw.rs | 2 -- .../apps/indexer/sources/src/command/process.rs | 2 -- application/apps/indexer/sources/src/lib.rs | 4 ++-- .../apps/indexer/sources/src/serial/serialport.rs | 2 -- application/apps/indexer/sources/src/socket/tcp.rs | 2 -- application/apps/indexer/sources/src/socket/udp.rs | 2 -- application/apps/rustcore/rs-bindings/Cargo.lock | 1 - 11 files changed, 2 insertions(+), 30 deletions(-) diff --git a/application/apps/indexer/Cargo.lock b/application/apps/indexer/Cargo.lock index e7cbfaebfa..b9ffae6728 100644 --- a/application/apps/indexer/Cargo.lock +++ b/application/apps/indexer/Cargo.lock @@ -135,17 +135,6 @@ dependencies = [ "syn 2.0.59", ] -[[package]] -name = "async-trait" -version = "0.1.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.59", -] - [[package]] name = "atty" version = "0.2.14" @@ -2102,7 +2091,6 @@ name = "sources" version = "0.1.0" dependencies = [ "async-stream", - "async-trait", "buf_redux 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", "bytes", "env_logger", diff --git a/application/apps/indexer/sources/Cargo.toml b/application/apps/indexer/sources/Cargo.toml index 94171bf1e8..e7d482ec50 100644 --- a/application/apps/indexer/sources/Cargo.toml +++ b/application/apps/indexer/sources/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" [dependencies] async-stream = "0.3" -async-trait = "0.1" buf_redux.workspace = true bytes = "1.3" etherparse = "0.13" diff --git a/application/apps/indexer/sources/src/binary/pcap/legacy.rs b/application/apps/indexer/sources/src/binary/pcap/legacy.rs index 9c8bb26896..8704d332b0 100644 --- a/application/apps/indexer/sources/src/binary/pcap/legacy.rs +++ b/application/apps/indexer/sources/src/binary/pcap/legacy.rs @@ -2,7 +2,6 @@ use crate::{ binary::pcap::debug_block, ByteSource, Error as SourceError, ReloadInfo, SourceFilter, TransportProtocol, }; -use async_trait::async_trait; use buf_redux::Buffer; use log::{debug, error, trace}; use pcap_parser::{traits::PcapReaderIterator, LegacyPcapReader, PcapBlockOwned, PcapError}; @@ -27,7 +26,6 @@ impl PcapLegacyByteSource { } } -#[async_trait] impl ByteSource for PcapLegacyByteSource { async fn reload( &mut self, diff --git a/application/apps/indexer/sources/src/binary/pcap/ng.rs b/application/apps/indexer/sources/src/binary/pcap/ng.rs index fd50aefd10..20bc6470ac 100644 --- a/application/apps/indexer/sources/src/binary/pcap/ng.rs +++ b/application/apps/indexer/sources/src/binary/pcap/ng.rs @@ -2,7 +2,6 @@ use crate::{ binary::pcap::debug_block, ByteSource, Error as SourceError, ReloadInfo, SourceFilter, TransportProtocol, }; -use async_trait::async_trait; use buf_redux::Buffer; use log::{debug, error, trace}; use pcap_parser::{traits::PcapReaderIterator, PcapBlockOwned, PcapError, PcapNGReader}; @@ -27,7 +26,6 @@ impl PcapngByteSource { } } -#[async_trait] impl ByteSource for PcapngByteSource { async fn reload( &mut self, diff --git a/application/apps/indexer/sources/src/binary/raw.rs b/application/apps/indexer/sources/src/binary/raw.rs index d69075274b..2e621c5b00 100644 --- a/application/apps/indexer/sources/src/binary/raw.rs +++ b/application/apps/indexer/sources/src/binary/raw.rs @@ -2,7 +2,6 @@ use crate::{ ByteSource, Error as SourceError, ReloadInfo, SourceFilter, DEFAULT_MIN_BUFFER_SPACE, DEFAULT_READER_CAPACITY, }; -use async_trait::async_trait; use buf_redux::{policy::MinBuffered, BufReader as ReduxReader}; use std::io::{BufRead, Read, Seek}; @@ -34,7 +33,6 @@ where } } -#[async_trait] impl ByteSource for BinaryByteSource { async fn reload( &mut self, diff --git a/application/apps/indexer/sources/src/command/process.rs b/application/apps/indexer/sources/src/command/process.rs index 5edf14246e..053ef4b414 100644 --- a/application/apps/indexer/sources/src/command/process.rs +++ b/application/apps/indexer/sources/src/command/process.rs @@ -1,5 +1,4 @@ use crate::{sde, ByteSource, Error as SourceError, ReloadInfo, SourceFilter}; -use async_trait::async_trait; use buf_redux::Buffer; use futures; use regex::{Captures, Regex}; @@ -154,7 +153,6 @@ impl ProcessSource { } } -#[async_trait] impl ByteSource for ProcessSource { async fn reload( &mut self, diff --git a/application/apps/indexer/sources/src/lib.rs b/application/apps/indexer/sources/src/lib.rs index 0861a1314a..e965d4cf8d 100644 --- a/application/apps/indexer/sources/src/lib.rs +++ b/application/apps/indexer/sources/src/lib.rs @@ -1,5 +1,4 @@ #![deny(unused_crate_dependencies)] -use async_trait::async_trait; use thiserror::Error; #[macro_use] @@ -77,6 +76,8 @@ pub enum Error { pub(crate) const DEFAULT_READER_CAPACITY: usize = 10 * 1024 * 1024; pub(crate) const DEFAULT_MIN_BUFFER_SPACE: usize = 10 * 1024; +// Warning can be suppressed here because we are using this trait in our own codebase only. +#[allow(async_fn_in_trait)] /// A `ByteSource` provides a way to read data from some underlying data source. But it does /// not provide a simple read interface, rather it allows implementations to filter the data /// while reading it from it's underlying source. @@ -84,7 +85,6 @@ pub(crate) const DEFAULT_MIN_BUFFER_SPACE: usize = 10 * 1024; /// want to extract the data part from certain frames, the `relaod` method will load only the relevant /// data into an internal buffer. /// This data can then be accessed via the `current_slice` method. -#[async_trait] pub trait ByteSource: Send + Sync { /// Indicate that we have consumed a certain amount of data from our internal /// buffer and that this part can be discarded diff --git a/application/apps/indexer/sources/src/serial/serialport.rs b/application/apps/indexer/sources/src/serial/serialport.rs index 8c82830fba..d62d5454fd 100644 --- a/application/apps/indexer/sources/src/serial/serialport.rs +++ b/application/apps/indexer/sources/src/serial/serialport.rs @@ -1,7 +1,6 @@ use crate::{ factory::SerialTransportConfig, sde, ByteSource, Error as SourceError, ReloadInfo, SourceFilter, }; -use async_trait::async_trait; use buf_redux::Buffer; use bytes::{BufMut, BytesMut}; use futures::{ @@ -131,7 +130,6 @@ impl SerialSource { } } -#[async_trait] impl ByteSource for SerialSource { async fn reload( &mut self, diff --git a/application/apps/indexer/sources/src/socket/tcp.rs b/application/apps/indexer/sources/src/socket/tcp.rs index 29db8c9777..802e4c1c03 100644 --- a/application/apps/indexer/sources/src/socket/tcp.rs +++ b/application/apps/indexer/sources/src/socket/tcp.rs @@ -1,5 +1,4 @@ use crate::{ByteSource, Error as SourceError, ReloadInfo, SourceFilter}; -use async_trait::async_trait; use buf_redux::Buffer; use tokio::net::{TcpStream, ToSocketAddrs}; @@ -21,7 +20,6 @@ impl TcpSource { } } -#[async_trait] impl ByteSource for TcpSource { async fn reload( &mut self, diff --git a/application/apps/indexer/sources/src/socket/udp.rs b/application/apps/indexer/sources/src/socket/udp.rs index 2f52efec19..22fb0c5a5b 100644 --- a/application/apps/indexer/sources/src/socket/udp.rs +++ b/application/apps/indexer/sources/src/socket/udp.rs @@ -1,5 +1,4 @@ use crate::{ByteSource, Error as SourceError, ReloadInfo, SourceFilter}; -use async_trait::async_trait; use buf_redux::Buffer; use indexer_base::config::MulticastInfo; use log::trace; @@ -75,7 +74,6 @@ impl UdpSource { } } -#[async_trait] impl ByteSource for UdpSource { async fn reload( &mut self, diff --git a/application/apps/rustcore/rs-bindings/Cargo.lock b/application/apps/rustcore/rs-bindings/Cargo.lock index 56cf3e3c13..27858c01ec 100644 --- a/application/apps/rustcore/rs-bindings/Cargo.lock +++ b/application/apps/rustcore/rs-bindings/Cargo.lock @@ -2493,7 +2493,6 @@ name = "sources" version = "0.1.0" dependencies = [ "async-stream", - "async-trait", "buf_redux 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", "bytes", "etherparse",