Skip to content

Commit

Permalink
feat: Add JanetArgs::get_value and JanetArgs::get_tagged trait me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
GrayJack committed Apr 22, 2024
1 parent 2aa2e1d commit bb0e280
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,11 @@ string_impl_partial_ord!(JanetKeyword<'_>, &'a bstr::BString);
/// Trait that only exist to extend methods over `[Janet]` so it's easier to get
/// [`janet_fn`](crate::janet_fn) args.
pub trait JanetArgs {
/// Get the argument at the `index` position.
fn get_value(&self, index: usize) -> Option<Janet>;

/// Get the argument at the `index` position as the [`TaggedJanet`] type.
fn get_tagged(&self, index: usize) -> Option<TaggedJanet>;
/// Get the argument at the `index` position and tries to convert to `T`.
fn get_unwrapped<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error>;

Expand Down Expand Up @@ -1891,6 +1896,14 @@ impl JanetArgs for [Janet] {
None => crate::jpanic!("bad slot #{}, there is no value in this slot", index),
}
}

fn get_value(&self, index: usize) -> Option<Janet> {
self.get(index).copied()
}

fn get_tagged(&self, index: usize) -> Option<TaggedJanet> {
self.get(index).map(|j| j.unwrap())
}
}

/// Trait defining the name of the type known to Janet
Expand Down

0 comments on commit bb0e280

Please sign in to comment.