From 8cf6b3086d30dd5c06ca61458a747edc45f76b4a Mon Sep 17 00:00:00 2001 From: GrayJack Date: Mon, 22 Apr 2024 16:59:17 -0300 Subject: [PATCH] refactor: Rename `JanetArgs::get_unwrapped` to `JanetArgs::try_get` Breaking change --- src/types.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/types.rs b/src/types.rs index 0909bced5e..8d939afd16 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1786,8 +1786,25 @@ pub trait JanetArgs { /// Get the argument at the `index` position as the [`TaggedJanet`] type. fn get_tagged(&self, index: usize) -> Option; + /// Get the argument at the `index` position and tries to convert to `T`. - fn get_unwrapped>(&self, index: usize) -> Result; + /// + /// # Examples + /// + /// ``` + /// use janetrs::{janet_fn, Janet, JanetArgs}; + /// + /// // Lets say it's a function that if receives an argument, if is not the wanted type, it + /// // defaults to the given value. + /// #[janet_fn(arity(range(0, 1)))] + /// fn my_func(args: &mut [Janet]) -> Janet { + /// let res = args.try_get::(0); + /// + /// // Rest of the function + /// todo!() + /// } + /// ``` + fn try_get>(&self, index: usize) -> Result; /// Get the argument at the `index` position and convert to `T`, if that fails, /// returns the `default` value. @@ -1795,7 +1812,7 @@ pub trait JanetArgs { /// # Examples /// /// ``` - /// use janetrs::{bad_slot, janet_fn, Janet, JanetArgs}; + /// use janetrs::{janet_fn, Janet, JanetArgs}; /// /// // Lets say it's a function that if receives an argument, if is not the wanted type, it /// // defaults to the given value. @@ -1819,7 +1836,7 @@ pub trait JanetArgs { /// # Examples /// /// ``` - /// use janetrs::{bad_slot, janet_fn, Janet, JanetArgs}; + /// use janetrs::{janet_fn, Janet, JanetArgs}; /// /// // Lets say it's a function that receives a second argument that change de behavior of /// // the function @@ -1842,7 +1859,7 @@ pub trait JanetArgs { /// # Examples /// /// ``` - /// use janetrs::{bad_slot, janet_fn, Janet, JanetArgs, JanetString}; + /// use janetrs::{janet_fn, Janet, JanetArgs, JanetString}; /// /// #[janet_fn(arity(fix(1)))] /// fn my_func(args: &mut [Janet]) -> Janet { @@ -1856,7 +1873,7 @@ pub trait JanetArgs { } impl JanetArgs for [Janet] { - fn get_unwrapped>(&self, index: usize) -> Result { + fn try_get>(&self, index: usize) -> Result { T::try_from(*self.get(index).unwrap_or(&Janet::nil())) }