Skip to content
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

Parse futures #2189

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl<'a> ApiDartGeneratorInfoTrait for DelegateApiDartGenerator<'a> {
ApiDartGenerator::new(mir.api_type.clone(), self.context).dart_api_type()
}
MirTypeDelegate::CustomSerDes(mir) => mir.info.dart_api_type.clone(),
MirTypeDelegate::Future(_) => "NOT_USED".to_string(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
MirTypeDelegate::CustomSerDes(mir) => {
mir.info.dart2rust.dart_code.replace("{}", "self")
}
MirTypeDelegate::Future(_) => unreachable!(),
},
Lang::RustLang(_) => match &self.mir {
MirTypeDelegate::Array(_) => {
Expand Down Expand Up @@ -109,6 +110,8 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
"flutter_rust_bridge::for_generated::rust_auto_opaque_explicit_encode(self)"
.to_owned()
}
MirTypeDelegate::Future(_) => unreachable!(),

MirTypeDelegate::ProxyVariant(_)
| MirTypeDelegate::ProxyEnum(_)
| MirTypeDelegate::DynTrait(_)
Expand Down Expand Up @@ -173,6 +176,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
| MirTypeDelegate::ProxyEnum(_) => {
return Some(format!("{};", lang.throw_unreachable("")));
}
MirTypeDelegate::Future(_) => unreachable!(),
MirTypeDelegate::BigPrimitive(_) => "BigInt.parse(inner)".to_owned(),
MirTypeDelegate::CastedPrimitive(_) => "inner.toInt()".to_owned(),
MirTypeDelegate::RustAutoOpaqueExplicit(_ir) => "inner".to_owned(),
Expand Down Expand Up @@ -225,6 +229,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
"flutter_rust_bridge::for_generated::rust_auto_opaque_explicit_decode(inner)"
.to_owned()
}
MirTypeDelegate::Future(_) => unreachable!(),
MirTypeDelegate::ProxyVariant(_)
| MirTypeDelegate::ProxyEnum(_)
| MirTypeDelegate::DynTrait(_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ impl<'a> WireDartCodecCstGeneratorEncoderTrait for DelegateWireDartCodecCstGener
| MirTypeDelegate::CastedPrimitive(_)
| MirTypeDelegate::CustomSerDes(_)
| MirTypeDelegate::Lifetimeable(_) =>
Acc::distribute(Some("throw UnimplementedError('Not implemented in this codec, please use the other one');".to_string()))
Acc::distribute(Some("throw UnimplementedError('Not implemented in this codec, please use the other one');".to_string())),
MirTypeDelegate::Future(_) => unreachable!()
,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl<'a> WireDartCodecDcoGeneratorDecoderTrait for DelegateWireDartCodecDcoGener
| MirTypeDelegate::CustomSerDes(_)
| MirTypeDelegate::Lifetimeable(_) =>
"throw UnimplementedError('Not implemented in this codec, please use the other one');".into(),
MirTypeDelegate::Future(_) => unreachable!()

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for DelegateWireRustCodecCstGener
io: Some("flutter_rust_bridge::for_generated::rust_auto_opaque_explicit_decode(self.cst_decode())".into()),
..Default::default()
},

// Do not care about these unimplemented things
// frb-coverage:ignore-start
MirTypeDelegate::ProxyVariant(_)
Expand All @@ -111,6 +112,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for DelegateWireRustCodecCstGener
MirTypeDelegate::CastedPrimitive(_)
| MirTypeDelegate::CustomSerDes(_)
| MirTypeDelegate::Lifetimeable(_) => Acc::distribute(None),
MirTypeDelegate::Future(_) => unreachable!()
// frb-coverage:ignore-end
}
}
Expand Down Expand Up @@ -157,6 +159,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for DelegateWireRustCodecCstGener
MirTypeDelegate::CastedPrimitive(_)
| MirTypeDelegate::CustomSerDes(_)
| MirTypeDelegate::Lifetimeable(_) => return None,
MirTypeDelegate::Future(_) => unreachable!()
// frb-coverage:ignore-end
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl<'a> WireRustGeneratorMiscTrait for DelegateWireRustGenerator<'a> {
MirTypeDelegate::ProxyEnum(mir) => Some(mir.get_delegate().rust_api_type()),
MirTypeDelegate::DynTrait(mir) => Some(mir.get_delegate().rust_api_type()),
MirTypeDelegate::Lifetimeable(mir) => Some(mir.delegate.inner.rust_api_type()),
MirTypeDelegate::Future(mir) => Some(mir.output.rust_api_type()),
_ => None,
}
}
Expand Down
19 changes: 19 additions & 0 deletions frb_codegen/src/library/codegen/ir/mir/ty/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum MirTypeDelegate {
DynTrait(MirTypeDelegateDynTrait),
Lifetimeable(MirTypeDelegateLifetimeable),
CustomSerDes(MirTypeDelegateCustomSerDes),
Future(MirTypeDelegateFuture)
fzyzcjy marked this conversation as resolved.
Show resolved Hide resolved
}

pub struct MirTypeDelegateArray {
Expand Down Expand Up @@ -75,6 +76,10 @@ pub struct MirTypeDelegateSet {
pub inner: Box<MirType>,
}

pub struct MirTypeDelegateFuture {
pub output: Box<MirType>,
}

pub struct MirTypeDelegateStreamSink {
pub inner_ok: Box<MirType>,
pub inner_err: Box<MirType>,
Expand Down Expand Up @@ -199,6 +204,9 @@ impl MirTypeTrait for MirTypeDelegate {
MirTypeDelegate::CustomSerDes(mir) => {
format!("CustomSerializer_{}", mir.info.rust_api_type.safe_ident())
}
MirTypeDelegate::Future(mir) => {
format!("Future_{}", mir.output.safe_ident())
}
}
}

Expand Down Expand Up @@ -271,6 +279,7 @@ impl MirTypeTrait for MirTypeDelegate {
MirTypeDelegate::ProxyEnum(mir) => mir.original.rust_api_type(),
MirTypeDelegate::Lifetimeable(mir) => mir.api_type.rust_api_type(),
MirTypeDelegate::CustomSerDes(mir) => mir.info.rust_api_type.rust_api_type(),
MirTypeDelegate::Future(mir) => mir.output.rust_api_type(),
}
}

Expand All @@ -288,6 +297,7 @@ impl MirTypeTrait for MirTypeDelegate {
MirTypeDelegate::Array(inner) => Some(inner.namespace.clone()),
MirTypeDelegate::ProxyEnum(inner) => inner.original.self_namespace(),
MirTypeDelegate::ProxyVariant(inner) => inner.inner.self_namespace(),
MirTypeDelegate::Future(inner) => inner.output.self_namespace(),
_ => None,
}
}
Expand Down Expand Up @@ -349,6 +359,7 @@ impl MirTypeDelegate {
MirTypeDelegate::RustAutoOpaqueExplicit(mir.delegate.clone()),
),
MirTypeDelegate::CustomSerDes(mir) => *mir.info.inner_type.clone(),
MirTypeDelegate::Future(mir) => *mir.output.clone(),
}
}
}
Expand Down Expand Up @@ -437,3 +448,11 @@ impl MirTypeDelegateDynTrait {
self.data.as_ref().unwrap()
}
}

impl MirTypeDelegateFuture {
pub fn new(output: MirType) -> Self {
Self {
output: Box::new(output),
}
}
}
4 changes: 2 additions & 2 deletions frb_codegen/src/library/codegen/ir/mir/ty/rust_opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use quote::ToTokens;
use regex::Regex;
use serde::{Deserialize, Serialize, Serializer};
use strum_macros::{Display, EnumIter};
use syn::Type;
use syn::GenericArgument;

crate::mir! {
pub struct MirTypeRustOpaque {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl MirRustOpaqueInner {
pub struct NameComponent {
pub ident: String,
#[serde(serialize_with = "serialize_vec_syn")]
pub args: Vec<Type>,
pub args: Vec<GenericArgument>,
}

fn serialize_vec_syn<T: ToTokens, S: Serializer>(values: &[T], s: S) -> Result<S::Ok, S::Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
return Ok(create_output_skip(func, ignore_func));
}

let rust_async = info.is_async || func.item_fn.sig().asyncness.is_some();

Ok(IrValueOrSkip::Value(MirFunc {
name: NamespacedName::new(namespace_refined, func_name),
dart_name,
Expand All @@ -250,7 +252,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
owner,
mode,
stream_dart_await,
rust_async: func.item_fn.sig().asyncness.is_some(),
rust_async,
initializer: attributes.init(),
accessor,
arg_mode: if attributes.positional() {
Expand Down Expand Up @@ -321,6 +323,7 @@ pub(crate) fn parse_effective_function_name_of_method(method: &MirFuncOwnerInfoM

#[derive(Debug, Default)]
struct FunctionPartialInfo {
is_async: bool,
inputs: Vec<MirFuncInput>,
ok_output: Option<MirType>,
error_output: Option<MirType>,
Expand All @@ -331,6 +334,7 @@ struct FunctionPartialInfo {
impl FunctionPartialInfo {
fn merge(self, other: Self) -> anyhow::Result<Self> {
Ok(Self {
is_async: self.is_async || other.is_async,
inputs: concat([self.inputs, other.inputs]),
ok_output: merge_option(self.ok_output, other.ok_output).context("ok_output type")?,
error_output: merge_option(self.error_output, other.error_output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,20 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
attributes: &FrbAttributes,
) -> anyhow::Result<FunctionPartialInfo> {
let mir = self.type_parser.parse_type(ty, context)?;

let (mir, is_async) =
if let MirType::Delegate(MirTypeDelegate::Future(delegate_future)) = &mir {
(*delegate_future.output.clone(), true)
} else {
(mir, false)
};

let mir = parse_maybe_proxy_return_type(mir, owner, attributes)?;
let info = parse_type_maybe_result(&mir, self.type_parser, context)?;
Ok(FunctionPartialInfo {
ok_output: Some(info.ok_output),
error_output: info.error_output,
is_async,
..Default::default()
})
}
Expand Down Expand Up @@ -116,6 +125,7 @@ fn parse_proxy_return_type(mir: MirType, owner: &MirFuncOwnerInfo) -> anyhow::Re
}
}
}

// This will stop the whole generator and tell the users, so we do not care about testing it
// frb-coverage:ignore-start
bail!("This return type is not currently compatible with `#[frb(proxy)]` yet")
Expand Down
Loading
Loading