From 64395245a524dcf478f714ae4124e9b2aff23148 Mon Sep 17 00:00:00 2001 From: Clement Rey Date: Wed, 28 Feb 2024 12:14:24 +0100 Subject: [PATCH] implement rust log_file example --- Cargo.lock | 36 ++++++++++++------ examples/rust/log_file/Cargo.toml | 14 +++++++ examples/rust/log_file/README.md | 10 +++++ examples/rust/log_file/src/main.rs | 59 ++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 examples/rust/log_file/Cargo.toml create mode 100644 examples/rust/log_file/README.md create mode 100644 examples/rust/log_file/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 89ff3e18e9070..cc0a5a7d7995e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2978,6 +2978,16 @@ dependencies = [ "rerun", ] +[[package]] +name = "log_file" +version = "0.14.0-alpha.2" +dependencies = [ + "anyhow", + "clap", + "glam", + "rerun", +] + [[package]] name = "lz4_flex" version = "0.11.1" @@ -3963,15 +3973,16 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.20.2" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a89dc7a5850d0e983be1ec2a463a171d20990487c3cfcd68b5363f1ee3d6fe0" +checksum = "53bdbb96d49157e65d45cc287af5f32ffadd5f4761438b527b055fb0d4bb8233" dependencies = [ "cfg-if", "indoc", "libc", "memoffset 0.9.0", "parking_lot", + "portable-atomic", "pyo3-build-config", "pyo3-ffi", "pyo3-macros", @@ -3980,9 +3991,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.20.2" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07426f0d8fe5a601f26293f300afd1a7b1ed5e78b2a705870c5f30893c5163be" +checksum = "deaa5745de3f5231ce10517a1f5dd97d53e5a2fd77aa6b5842292085831d48d7" dependencies = [ "once_cell", "target-lexicon", @@ -3990,9 +4001,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.20.2" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb7dec17e17766b46bca4f1a4215a85006b4c2ecde122076c562dd058da6cf1" +checksum = "62b42531d03e08d4ef1f6e85a2ed422eb678b8cd62b762e53891c05faf0d4afa" dependencies = [ "libc", "pyo3-build-config", @@ -4000,9 +4011,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.20.2" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f738b4e40d50b5711957f142878cfa0f28e054aa0ebdfc3fd137a843f74ed3" +checksum = "7305c720fa01b8055ec95e484a6eca7a83c841267f0dd5280f0c8b8551d2c158" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -4012,12 +4023,13 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.20.2" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fc910d4851847827daf9d6cdd4a823fbdaab5b8818325c5e97a86da79e8881f" +checksum = "7c7e9b68bb9c3149c5b0cade5d07f953d6d125eb4337723c4ccdb665f1f96185" dependencies = [ "heck 0.4.1", "proc-macro2", + "pyo3-build-config", "quote", "syn 2.0.48", ] @@ -6155,9 +6167,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.12.13" +version = "0.12.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69758bda2e78f098e4ccb393021a0963bb3442eac05f135c30f61b7370bbafae" +checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" [[package]] name = "tempfile" diff --git a/examples/rust/log_file/Cargo.toml b/examples/rust/log_file/Cargo.toml new file mode 100644 index 0000000000000..e21f1075efcca --- /dev/null +++ b/examples/rust/log_file/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "log_file" +version = "0.14.0-alpha.2" +edition = "2021" +rust-version = "1.74" +license = "MIT OR Apache-2.0" +publish = false + +[dependencies] +rerun = { path = "../../../crates/rerun", features = ["web_viewer", "clap"] } + +anyhow = "1.0" +clap = { version = "4.0", features = ["derive"] } +glam = "0.22" diff --git a/examples/rust/log_file/README.md b/examples/rust/log_file/README.md new file mode 100644 index 0000000000000..f07e5ca78459a --- /dev/null +++ b/examples/rust/log_file/README.md @@ -0,0 +1,10 @@ + + +Demonstrates how to log any file from the SDK using the [`DataLoader`](https://www.rerun.io/docs/howto/open-any-file) machinery. + +Usage: +```bash +cargo run -p log_file -- examples/assets +``` diff --git a/examples/rust/log_file/src/main.rs b/examples/rust/log_file/src/main.rs new file mode 100644 index 0000000000000..cf4a4e92719af --- /dev/null +++ b/examples/rust/log_file/src/main.rs @@ -0,0 +1,59 @@ +//! Demonstrates how to log any file from the SDK using the `DataLoader` machinery. +//! +//! See for more information. +//! +//! Usage: +//! ``` +//! cargo run -p log_file -- examples/assets +//! ``` + +use rerun::external::re_log; + +#[derive(Debug, clap::Parser)] +#[clap(author, version, about)] +struct Args { + #[command(flatten)] + rerun: rerun::clap::RerunArgs, + + // Log the contents of the file directly (files only -- not supported by external loaders). + #[clap(long, default_value = "false")] + from_contents: bool, + + /// The filepaths to be loaded and logged. + filepaths: Vec, +} + +fn main() -> anyhow::Result<()> { + re_log::setup_logging(); + + use clap::Parser as _; + let args = Args::parse(); + + let (rec, _serve_guard) = args.rerun.init("rerun_example_log_file")?; + run(&rec, &args)?; + + Ok(()) +} + +fn run(rec: &rerun::RecordingStream, args: &Args) -> anyhow::Result<()> { + for filepath in &args.filepaths { + let filepath = filepath.as_path(); + + if !args.from_contents { + // Either log the file using its path… + rec.log_file_from_path(filepath)?; + } else { + // …or using its contents if you already have them loaded for some reason. + if filepath.is_file() { + let contents = std::fs::read(filepath)?; + rec.log_file_from_contents(filepath, std::borrow::Cow::Borrowed(&contents))?; + } + } + } + + // TODO(cmc): This is required because the way we handle RecordingStream clones in subtly + // broken. That's outside the scope of this PR, I'll fix this in a follow-up. + rec.disconnect(); + + Ok(()) +}