Skip to content

Commit

Permalink
Merge pull request #36 from vertexclique/attributes
Browse files Browse the repository at this point in the history
Attributes
  • Loading branch information
vertexclique authored Jan 25, 2024
2 parents c465486 + c1e02e2 commit 2697647
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 244 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ autoexamples = false


[features]
default = ["async-exec", "iouring"]
default = ["async-exec", "iouring", "attributes"]
# Devs should enable these features to use iouring on linux.
#default = ["asyncstd", "iouring"]
epoll = []
Expand All @@ -41,7 +41,10 @@ iouring = ["rustix-uring", "rustix"]
async-exec = ["async-global-executor"]
tokio = ["async-global-executor/tokio"]

attributes = ["nuclei-attributes"]

[dependencies]
nuclei-attributes = { version = "0.1", optional = true }
lever = "0.1"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
socket2 = { version = "0.3.19", features = ["pair", "unix"] }
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dev-dependencies]
#nuclei = { path = "../" }
nuclei = { path = "../", default-features = false, features = ["tokio", "iouring"]}
nuclei = { path = "../", default-features = false, features = ["tokio", "iouring", "attributes"]}
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
futures-util = "0.3"
anyhow = "1.0.31"
Expand Down
45 changes: 22 additions & 23 deletions examples/fread-vect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@ use futures::AsyncReadExt;

const IOVEC_WIDTH: usize = 1 << 10;

fn main() -> io::Result<()> {
let x = drive(async {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("quark-gluon-plasma");

let mut buf1 = [0; IOVEC_WIDTH];
let mut buf2 = [0; IOVEC_WIDTH];
let mut buf3 = [0; IOVEC_WIDTH];
let mut bufs = [
IoSliceMut::new(&mut buf1),
IoSliceMut::new(&mut buf2),
IoSliceMut::new(&mut buf3),
];

let fo = File::open(&path).unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
file.read_vectored(&mut bufs[..]).await.unwrap();

vec![buf1, buf2, buf3]
});

x.iter().enumerate().for_each(|(idx, e)| {
#[nuclei::main]
async fn main() -> io::Result<()> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("quark-gluon-plasma");

let mut buf1 = [0; IOVEC_WIDTH];
let mut buf2 = [0; IOVEC_WIDTH];
let mut buf3 = [0; IOVEC_WIDTH];
let mut bufs = [
IoSliceMut::new(&mut buf1),
IoSliceMut::new(&mut buf2),
IoSliceMut::new(&mut buf3),
];

let fo = File::open(&path).unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
file.read_vectored(&mut bufs[..]).await.unwrap();

let bufs = vec![buf1, buf2, buf3];

bufs.iter().enumerate().for_each(|(idx, e)| {
println!(
"::: iovec ::: {}, data ::: \n\n{}\n\n",
idx,
Expand Down
26 changes: 11 additions & 15 deletions examples/fread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ use std::path::PathBuf;

use futures::AsyncReadExt;

fn main() -> io::Result<()> {
let x: io::Result<String> = drive(async {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("quark-gluon-plasma");
dbg!(&path);
#[nuclei::main]
async fn main() -> io::Result<()> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("quark-gluon-plasma");

let fo = File::open(&path).unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
let mut buffer = String::new();
file.read_to_string(&mut buffer).await?;
Ok(buffer)
});
let x = x?;
let fo = File::open(&path).unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
let mut buffer = String::new();
file.read_to_string(&mut buffer).await?;

println!("Content: {}", x);
println!("Length of file is {}", x.len());
println!("Content: {}", buffer);
println!("Length of file is {}", buffer.len());

Ok(())
}
67 changes: 33 additions & 34 deletions examples/fwrite-vect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,39 @@ use std::io::{IoSlice, SeekFrom};

const IOVEC_WIDTH: usize = 1 << 10;

fn main() -> io::Result<()> {
let x = drive(async {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("dark-matter-vect");

let buf1 = [0x41; IOVEC_WIDTH];
let buf2 = [0x42; IOVEC_WIDTH];
let buf3 = [0x43; IOVEC_WIDTH];
let bufs = [
IoSlice::new(&buf1),
IoSlice::new(&buf2),
IoSlice::new(&buf3),
];

let fo = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
file.write_vectored(&bufs[..]).await.unwrap();

let mut bufv = String::new();
assert!(file.seek(SeekFrom::Start(0)).await.is_ok());
file.read_to_string(&mut bufv).await.unwrap();
bufv
});

assert_eq!(x.matches('A').count(), IOVEC_WIDTH);
assert_eq!(x.matches('B').count(), IOVEC_WIDTH);
assert_eq!(x.matches('C').count(), IOVEC_WIDTH);

println!("SG write was: {}", x);
#[nuclei::main]
async fn main() -> io::Result<()> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("dark-matter-vect");

let buf1 = [0x41; IOVEC_WIDTH];
let buf2 = [0x42; IOVEC_WIDTH];
let buf3 = [0x43; IOVEC_WIDTH];
let bufs = [
IoSlice::new(&buf1),
IoSlice::new(&buf2),
IoSlice::new(&buf3),
];

let fo = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
file.write_vectored(&bufs[..]).await.unwrap();

let mut bufv = String::new();
assert!(file.seek(SeekFrom::Start(0)).await.is_ok());
file.read_to_string(&mut bufv).await.unwrap();


assert_eq!(bufv.matches('A').count(), IOVEC_WIDTH);
assert_eq!(bufv.matches('B').count(), IOVEC_WIDTH);
assert_eq!(bufv.matches('C').count(), IOVEC_WIDTH);

println!("SG write was: {}", bufv);

Ok(())
}
43 changes: 21 additions & 22 deletions examples/fwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,30 @@ to detect.[1]\
\
";

fn main() -> io::Result<()> {
#[nuclei::main]
async fn main() -> io::Result<()> {
// Approximately ~75,9 MB
let dark_matter = vec![DARK_MATTER_TEXT; 100_000].join("\n");

let x = drive(async {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("dark-matter");

let fo = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
file.write_all(dark_matter.as_bytes()).await.unwrap();

let mut buf = vec![];
assert!(file.seek(SeekFrom::Start(0)).await.is_ok());
assert_eq!(file.read_to_end(&mut buf).await.unwrap(), dark_matter.len());
assert_eq!(&buf[0..dark_matter.len()], dark_matter.as_bytes());
buf
});

println!("Length of file is {}", x.len());
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("data");
path.push("dark-matter");

let fo = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let mut file = Handle::<File>::new(fo).unwrap();
file.write_all(dark_matter.as_bytes()).await.unwrap();

let mut buf = vec![];
assert!(file.seek(SeekFrom::Start(0)).await.is_ok());
assert_eq!(file.read_to_end(&mut buf).await.unwrap(), dark_matter.len());
assert_eq!(&buf[0..dark_matter.len()], dark_matter.as_bytes());


println!("Length of file is {}", buf.len());

Ok(())
}
13 changes: 5 additions & 8 deletions examples/h1-server-multishot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(target_os = "linux")]
fn main() -> anyhow::Result<()> {
#[nuclei::main]
async fn main() -> anyhow::Result<()> {
use nuclei::*;
use std::net::TcpListener;

Expand Down Expand Up @@ -51,14 +52,10 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

spawn_blocking(|| drive(future::pending::<()>())).detach();
let http = listen(Handle::<TcpListener>::bind("0.0.0.0:8000")?);

block_on(async {
let http = listen(Handle::<TcpListener>::bind("0.0.0.0:8000")?);

http.await?;
Ok(())
})
http.await?;
Ok(())
}

#[cfg(target_os = "macos")]
Expand Down
13 changes: 5 additions & 8 deletions examples/h1-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ async fn listen(listener: Handle<TcpListener>) -> Result<()> {
}
}

fn main() -> Result<()> {
spawn_blocking(|| drive(future::pending::<()>())).detach();
#[nuclei::main]
async fn main() -> Result<()> {
let http = listen(Handle::<TcpListener>::bind("0.0.0.0:8000")?);

block_on(async {
let http = listen(Handle::<TcpListener>::bind("0.0.0.0:8000")?);

http.await?;
Ok(())
})
http.await?;
Ok(())
}
17 changes: 8 additions & 9 deletions examples/tcp-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use nuclei::*;
use std::io;
use std::net::TcpStream;

fn main() -> io::Result<()> {
drive(async {
println!("Connecting to server");
let mut stream = Handle::<TcpStream>::connect("127.0.0.1:7000").await?;
println!("Connected to {}", stream.get_ref().peer_addr()?);
#[nuclei::main]
async fn main() -> io::Result<()> {
println!("Connecting to server");
let mut stream = Handle::<TcpStream>::connect("127.0.0.1:7000").await?;
println!("Connected to {}", stream.get_ref().peer_addr()?);

let result = stream.write(b"hello world\n").await;
println!("Wrote, success={:?}", result.is_ok());
let result = stream.write(b"hello world\n").await;
println!("Wrote, success={:?}", result.is_ok());

Ok(())
})
Ok(())
}
27 changes: 13 additions & 14 deletions examples/tcp-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ async fn echo(stream: Handle<TcpStream>) -> io::Result<()> {
Ok(())
}

fn main() -> io::Result<()> {
drive(async {
// Create a listener.
let listener = Handle::<TcpListener>::bind("0.0.0.0:7000")?;
println!("Listening on {}", listener.get_ref().local_addr()?);
println!("Now start a TCP client.");
#[nuclei::main]
async fn main() -> io::Result<()> {
// Create a listener.
let listener = Handle::<TcpListener>::bind("0.0.0.0:7000")?;
println!("Listening on {}", listener.get_ref().local_addr()?);
println!("Now start a TCP client.");

// Accept clients in a loop.
loop {
let (stream, peer_addr) = listener.accept().await?;
println!("Accepted client: {:?}", peer_addr);
// Accept clients in a loop.
loop {
let (stream, peer_addr) = listener.accept().await?;
println!("Accepted client: {:?}", peer_addr);

// Spawn a task that echoes messages from the client back to it.
spawn(echo(stream)).detach();
}
})
// Spawn a task that echoes messages from the client back to it.
spawn(echo(stream)).detach();
}
}
21 changes: 10 additions & 11 deletions examples/unix-signal.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#[cfg(unix)]
fn main() -> std::io::Result<()> {
#[nuclei::main]
async fn main() -> std::io::Result<()> {
use futures::prelude::*;
use nuclei::*;
use std::os::unix::net::UnixStream;

drive(async {
// Create a Unix stream that receives a byte on each signal occurrence.
let (a, mut b) = Handle::<UnixStream>::pair()?;
signal_hook::pipe::register(signal_hook::SIGINT, a)?;
println!("Waiting for Ctrl-C...");
// Create a Unix stream that receives a byte on each signal occurrence.
let (a, mut b) = Handle::<UnixStream>::pair()?;
signal_hook::pipe::register(signal_hook::SIGINT, a)?;
println!("Waiting for Ctrl-C...");

// Receive a byte that indicates the Ctrl-C signal occurred.
b.read_exact(&mut [0]).await?;
// Receive a byte that indicates the Ctrl-C signal occurred.
b.read_exact(&mut [0]).await?;

println!("Done!");
Ok(())
})
println!("Done!");
Ok(())
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ mod syscore {

pub use async_global_executor::*;
pub use proactor::*;

#[cfg(feature = "attributes")]
pub use nuclei_attributes::*;
2 changes: 1 addition & 1 deletion src/proactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn drive<T>(future: impl Future<Output = T>) -> T {
}

#[cfg(test)]
#[cfg(feature = "iouring")]
#[cfg(target_os = "linux")]
mod proactor_tests {
use crate::config::{IoUringConfiguration, NucleiConfig};
use crate::Proactor;
Expand Down
1 change: 0 additions & 1 deletion testdata/dark-matter
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 0 additions & 1 deletion testdata/dark-matter-vect
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

Loading

0 comments on commit 2697647

Please sign in to comment.