Skip to content

Commit

Permalink
Rust native support for 'ByteSource' async trait
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
AmmarAbouZor authored and marcmo committed Sep 16, 2024
1 parent 4d5c00b commit db2496f
Show file tree
Hide file tree
Showing 11 changed files with 2 additions and 30 deletions.
12 changes: 0 additions & 12 deletions application/apps/indexer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion application/apps/indexer/sources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/binary/pcap/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -27,7 +26,6 @@ impl<R: Read> PcapLegacyByteSource<R> {
}
}

#[async_trait]
impl<R: Read + Send + Sync> ByteSource for PcapLegacyByteSource<R> {
async fn reload(
&mut self,
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/binary/pcap/ng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -27,7 +26,6 @@ impl<R: Read> PcapngByteSource<R> {
}
}

#[async_trait]
impl<R: Read + Send + Sync> ByteSource for PcapngByteSource<R> {
async fn reload(
&mut self,
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/binary/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -34,7 +33,6 @@ where
}
}

#[async_trait]
impl<R: Read + Send + Sync + Seek> ByteSource for BinaryByteSource<R> {
async fn reload(
&mut self,
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/command/process.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -154,7 +153,6 @@ impl ProcessSource {
}
}

#[async_trait]
impl ByteSource for ProcessSource {
async fn reload(
&mut self,
Expand Down
4 changes: 2 additions & 2 deletions application/apps/indexer/sources/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![deny(unused_crate_dependencies)]
use async_trait::async_trait;
use thiserror::Error;

#[macro_use]
Expand Down Expand Up @@ -77,14 +76,15 @@ 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.
/// A good example is a network trace where complete ethernet frames are described. If we only
/// 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
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/serial/serialport.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -131,7 +130,6 @@ impl SerialSource {
}
}

#[async_trait]
impl ByteSource for SerialSource {
async fn reload(
&mut self,
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/socket/tcp.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -21,7 +20,6 @@ impl TcpSource {
}
}

#[async_trait]
impl ByteSource for TcpSource {
async fn reload(
&mut self,
Expand Down
2 changes: 0 additions & 2 deletions application/apps/indexer/sources/src/socket/udp.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -75,7 +74,6 @@ impl UdpSource {
}
}

#[async_trait]
impl ByteSource for UdpSource {
async fn reload(
&mut self,
Expand Down
1 change: 0 additions & 1 deletion application/apps/rustcore/rs-bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db2496f

Please sign in to comment.