-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
func type workarounds #399
Milestone
Comments
This seems to wotk, gettinh close: // TODO start custom Func implementation
// type BasicFunc = candid::Func;
/// A marker type to match unconstrained callback arguments
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
pub struct ArgToken;
impl CandidType for ArgToken {
fn _ty() -> candid::types::Type {
candid::types::Type::Empty
}
fn idl_serialize<S: candid::types::Serializer>(&self, _serializer: S) -> Result<(), S::Error> {
// We cannot implement serialize, since our type must be `Empty` in order to accept anything.
// Attempting to serialize this type is always an error and should be regarded as a compile time error.
unimplemented!("Token is not serializable")
}
}
#[derive(Debug, Clone)]
struct BasicFunc<ArgToken = self::ArgToken>(
pub candid::Func,
pub std::marker::PhantomData<ArgToken>,
);
impl AzleIntoJsValue for BasicFunc {
fn azle_into_js_value(self, context: &mut boa_engine::Context) -> boa_engine::JsValue {
self.0.azle_into_js_value(context)
}
}
impl AzleTryFromJsValue<BasicFunc<ArgToken>> for boa_engine::JsValue {
fn azle_try_from_js_value(self, context: &mut boa_engine::Context) -> Result<BasicFunc<ArgToken>, AzleTryFromJsValueError> {
let candid_func: candid::Func = self.azle_try_from_js_value(context).unwrap();
Ok(candid_func.into())
}
}
impl<ArgToken: CandidType> CandidType for BasicFunc<ArgToken> {
fn _ty() -> candid::types::Type {
// TODO this is the part that needs to be generated from the TypeScript type for type
// TODO basically go grab all of the Funcs, grab the query/update, and grab the args and return type
// TODO should not be too bad actually
candid::types::Type::Func(candid::types::Function {
modes: vec![candid::parser::types::FuncMode::Query],
args: vec![candid::types::Type::Text],
rets: vec![candid::types::Type::Text]
})
}
fn idl_serialize<S: candid::types::Serializer>(&self, serializer: S) -> Result<(), S::Error> {
self.0.idl_serialize(serializer)
}
}
impl<'de, ArgToken> Deserialize<'de> for BasicFunc<ArgToken> {
fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
candid::Func::deserialize(deserializer).map(Self::from)
}
}
impl<ArgToken> From<candid::Func> for BasicFunc<ArgToken> {
fn from(f: candid::Func) -> Self {
Self(f, std::marker::PhantomData)
}
}
impl<ArgToken> From<BasicFunc<ArgToken>> for candid::Func {
fn from(c: BasicFunc<ArgToken>) -> Self {
c.0
}
}
impl<ArgToken> std::ops::Deref for BasicFunc<ArgToken> {
type Target = candid::Func;
fn deref(&self) -> &candid::Func {
&self.0
}
}
impl<ArgToken> std::ops::DerefMut for BasicFunc<ArgToken> {
fn deref_mut(&mut self) -> &mut candid::Func {
&mut self.0
}
}
// TODO end custom Func implementation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: