Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 79 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[workspace]
default-members = ["humility-bin"]
members = [
"examples/*",
"humility-arch-arm",
"humility-arch-cortex",
"humility-auxflash",
Expand Down
16 changes: 16 additions & 0 deletions examples/async/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "humility-async"
version = "0.1.0"
edition.workspace = true

[dependencies]
async-trait = "0.1.89"
tokio = { version = "1", features = ["full"] }
humility.workspace = true
humility-probes-core.workspace = true
humility-hiffy.workspace = true
humility-idol.workspace = true
anyhow.workspace = true
idol.workspace = true
thiserror.workspace = true
slog.workspace = true
88 changes: 88 additions & 0 deletions examples/async/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/// Demonstrating using humiltiy as a library with async traits

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Demonstrating using humiltiy as a library with async traits
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/// Demonstrating using humiltiy as a library with async traits

///
/// This ended up being non-obvious when this was used in another project.
/// This is designed to be a straightfoward example to play around with.
use humility::hubris::HubrisArchive;
use humility_idol::HubrisIdol;
use humility_probes_core::HubrisAttach;
use slog::Logger;
use std::sync::{Arc, Mutex};

#[derive(Debug, thiserror::Error)]
enum WrappedError {
#[error("HUMILITY_ARCHIVE unset")]
HumilityArchiveUnset(#[source] std::env::VarError),
#[error("HUMILTIY_PROBE unset")]
HumilityProbeUnset(#[source] std::env::VarError),
#[error("Hubris archive load failed")]
HubrisArchive(#[source] anyhow::Error),
#[error("probe attach failed")]
ProbeAttach(#[source] anyhow::Error),
}

struct MyWrapper {
// This does not work! `HubrisArchive` cannot be `Sync` because
// other struct members like addr2line/gimli are not `Sync`.
// You can experiment to see the full error.
//hubris: HubrisArchive,
hubris: Arc<Mutex<HubrisArchive>>,
log: Logger,
}

impl MyWrapper {
fn new(log: &Logger) -> Result<Self, WrappedError> {
let hubris = std::env::var("HUMILITY_ARCHIVE")
.map_err(WrappedError::HumilityArchiveUnset)?;

let hubris = HubrisArchive::load_from_path(&hubris, &log)
.map_err(WrappedError::HubrisArchive)?;

let log = log.new(slog::o!());

Ok(Self { hubris: Arc::new(Mutex::new(hubris)), log })
}
}

#[async_trait::async_trait]
trait MyTrait {
async fn do_something(&self) -> Result<(), WrappedError>;
}

#[async_trait::async_trait]
impl MyTrait for MyWrapper {
async fn do_something(&self) -> Result<(), WrappedError> {
let probe = std::env::var("HUMILITY_PROBE")
.map_err(WrappedError::HumilityProbeUnset)?;

let hubris = self.hubris.lock().unwrap();
let mut core = hubris
.attach_probe(&probe, 8000, &self.log)
.map_err(WrappedError::ProbeAttach)?;

let get_op = hubris.get_idol_command("Sequencer.get_state").unwrap();

let mut context = humility_hiffy::HiffyContext::new(
&hubris,
&mut core,
std::time::Duration::from_millis(10000),
&self.log,
)
.unwrap();

let _state =
context.call::<u8>(&mut core, &get_op, &[], None, None).unwrap();

Ok(())
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let log = humility::log::init(false);

let wrapper = MyWrapper::new(&log).unwrap();

wrapper.do_something().await.unwrap();

Ok(())
}
Loading