#macro #async #write #read #events #reader-writer #hepmc2

macro hepmc2-macros

为hepmc2crate提供的宏

1 个不稳定版本

0.1.0 2024年4月24日

#109 in #reader-writer


4 个crate中使用 (通过 hepmc2)

GPL-3.0-or-later

5KB
87

hepmc2

hepmc2 格式(也称为 IO_GenEvent)中读取和写入事件文件。

注意事项

此crate受HepMC3库中 ReaderAsciiHepMC2 代码的启发,版本3.2.0。目标是完全兼容,但请注意,当前测试并非详尽无遗。

示例

// Read events from `events_in.hepmc2` and write them to `events_out.hepmc2`
use hepmc2::{Reader, Writer};

use std::io::BufReader;
use std::fs::File;

let input = BufReader::new(File::open("events_in.hepmc2")?);
let in_events = Reader::from(input);

let output = File::create("events_out.hepmc2")?;
let mut writer = Writer::try_from(output)?;

for event in in_events {
   let event = event?;
   println!("Current cross section: {}",  event.xs);
   writer.write(&event)?
}
writer.finish()?;

异步API

默认情况下,此crate启用 sync 功能,该功能公开了同步API。但是,您可以通过禁用 sync 功能并启用 tokio 功能来切换到使用由 tokio 支持的异步API。

在crate的根目录下运行以下命令

cargo add hepmc2 --no-default-features -F tokio

或者确保 Cargo.toml 中存在以下类似的行

hepmc2 = { version = "0.6.0", default-features = false, features = ["tokio"] }

异步API与同步API完全相同,但IO操作将返回futures,您需要像通常一样对它们调用 await

示例

// Read events from `events_in.hepmc2` and write them to `events_out.hepmc2`
use hepmc2::{Reader, Writer};

use tokio::io::BufReader;
use tokio::fs::File;

let input = BufReader::new(File::open("events_in.hepmc2").await?);
let mut in_events = Reader::from(input);

let output = File::create("events_out.hepmc2").await?;
let mut writer = Writer::try_from(output).await?;

while let Some(event) = in_events.next().await {
   let event = event?;
   println!("Current cross section: {}",  event.xs);
   writer.write(&event).await?
}
writer.finish().await?;

许可证:GPL-3.0-or-later

依赖关系

~270–720KB
~17K SLoC