Skip to content

Commit

Permalink
Merge pull request #2058 from fzyzcjy/feat/12197
Browse files Browse the repository at this point in the history
Automatically strip prefix `set_`/`get_` when it is a setter/getter function
  • Loading branch information
fzyzcjy authored Jun 10, 2024
2 parents 5557824 + 5850992 commit 9b49798
Show file tree
Hide file tree
Showing 16 changed files with 2,304 additions and 1,898 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::codegen::generator::codec::structs::{CodecMode, CodecModePack};
use crate::codegen::ir::hir::flat::function::HirFlatFunction;
use crate::codegen::ir::hir::flat::function::HirFlatFunctionOwner;
use crate::codegen::ir::mir::func::{
MirFunc, MirFuncArgMode, MirFuncImplMode, MirFuncImplModeDartOnly, MirFuncInput, MirFuncMode,
MirFuncOutput, MirFuncOwnerInfo, MirFuncOwnerInfoMethod, MirFuncOwnerInfoMethodMode,
MirFunc, MirFuncArgMode, MirFuncImplMode, MirFuncImplModeDartOnly,
MirFuncInput, MirFuncMode, MirFuncOutput, MirFuncOwnerInfo, MirFuncOwnerInfoMethod,
MirFuncOwnerInfoMethodMode,
};
use crate::codegen::ir::mir::skip::MirSkipReason::IgnoredFunctionGeneric;
use crate::codegen::ir::mir::skip::{MirSkip, MirSkipReason};
Expand Down Expand Up @@ -127,7 +128,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
let src_lineno = func.item_fn.span().start().line;
let attributes = FrbAttributes::parse(func.item_fn.attrs())?;

let dart_name = attributes.name();
let dart_name = parse_dart_name(&attributes, &func.item_fn.name());

let create_context = |owner: Option<MirFuncOwnerInfo>| TypeParserParsingContext {
initiated_namespace: func.namespace.clone(),
Expand Down Expand Up @@ -167,6 +168,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
let mode = compute_func_mode(&attributes, &info);
let stream_dart_await = attributes.stream_dart_await() && !attributes.sync();
let namespace_refined = refine_namespace(&owner).unwrap_or(func.namespace.clone());
let accessor = attributes.accessor();

let output = MirFuncOutput {
normal: info.ok_output.unwrap_or(Primitive(MirTypePrimitive::Unit)),
Expand All @@ -190,7 +192,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
stream_dart_await,
rust_async: func.item_fn.sig().asyncness.is_some(),
initializer: attributes.init(),
accessor: attributes.accessor(),
accessor,
arg_mode: if attributes.positional() {
MirFuncArgMode::Positional
} else {
Expand Down Expand Up @@ -430,3 +432,12 @@ fn compute_impl_mode(

MirFuncImplMode::Normal
}

fn parse_dart_name(attributes: &FrbAttributes, func_name_raw: &str) -> Option<String> {
(attributes.name()).or_else(|| {
(attributes.accessor()).and_then(|accessor| {
(func_name_raw.strip_prefix(&format!("{}_", accessor.verb_str())))
.map(ToString::to_string)
})
})
}
7 changes: 7 additions & 0 deletions frb_example/pure_dart/frb_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -4025,8 +4025,13 @@ void frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__Stru
void frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithImplBlockInMultiFile_method_in_b(int64_t port_,
uintptr_t that);

WireSyncRust2DartDco frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(uintptr_t that);

WireSyncRust2DartDco frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new(void);

WireSyncRust2DartDco frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(uintptr_t that,
int32_t value);

WireSyncRust2DartDco frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_getter(uintptr_t that);

WireSyncRust2DartDco frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_setter(uintptr_t that,
Expand Down Expand Up @@ -15773,7 +15778,9 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructInMiscNoTwinExampleA_sample_function_a);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithImplBlockInMultiFile_method_in_a);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithImplBlockInMultiFile_method_in_b);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_getter);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_setter);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__function_with_arg_type_name_override);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ abstract class StructWithImplBlockInMultiFile implements RustOpaqueInterface {

// Rust type: RustOpaqueNom<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<StructWithSimpleSetterTwinNormal>>
abstract class StructWithSimpleSetterTwinNormal implements RustOpaqueInterface {
int get something;

factory StructWithSimpleSetterTwinNormal() => RustLib.instance.api
.crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalNew();

void set something(int value);

int get simpleGetter;

void set simpleSetter(int value);
Expand Down
78 changes: 77 additions & 1 deletion frb_example/pure_dart/lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
String get codegenVersion => '2.0.0-dev.38';

@override
int get rustContentHash => 144283047;
int get rustContentHash => 143567850;

static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
Expand Down Expand Up @@ -1078,9 +1078,15 @@ abstract class RustLibApi extends BaseApi {
crateApiMiscNoTwinExampleAStructWithImplBlockInMultiFileMethodInB(
{required StructWithImplBlockInMultiFile that});

int crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalGetSomething(
{required StructWithSimpleSetterTwinNormal that});

StructWithSimpleSetterTwinNormal
crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalNew();

void crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSetSomething(
{required StructWithSimpleSetterTwinNormal that, required int value});

int crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSimpleGetter(
{required StructWithSimpleSetterTwinNormal that});

Expand Down Expand Up @@ -18455,6 +18461,36 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["that"],
);

@override
int crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalGetSomething(
{required StructWithSimpleSetterTwinNormal that}) {
return handler.executeSync(SyncTask(
callFfi: () {
var arg0 =
cst_encode_Auto_Ref_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerStructWithSimpleSetterTwinNormal(
that);
return wire
.wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
arg0);
},
codec: DcoCodec(
decodeSuccessData: dco_decode_i_32,
decodeErrorData: null,
),
constMeta:
kCrateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalGetSomethingConstMeta,
argValues: [that],
apiImpl: this,
));
}

TaskConstMeta
get kCrateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalGetSomethingConstMeta =>
const TaskConstMeta(
debugName: "StructWithSimpleSetterTwinNormal_get_something",
argNames: ["that"],
);

@override
StructWithSimpleSetterTwinNormal
crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalNew() {
Expand Down Expand Up @@ -18482,6 +18518,37 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: [],
);

@override
void crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSetSomething(
{required StructWithSimpleSetterTwinNormal that, required int value}) {
return handler.executeSync(SyncTask(
callFfi: () {
var arg0 =
cst_encode_Auto_RefMut_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerStructWithSimpleSetterTwinNormal(
that);
var arg1 = cst_encode_i_32(value);
return wire
.wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
arg0, arg1);
},
codec: DcoCodec(
decodeSuccessData: dco_decode_unit,
decodeErrorData: null,
),
constMeta:
kCrateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSetSomethingConstMeta,
argValues: [that, value],
apiImpl: this,
));
}

TaskConstMeta
get kCrateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSetSomethingConstMeta =>
const TaskConstMeta(
debugName: "StructWithSimpleSetterTwinNormal_set_something",
argNames: ["that", "value"],
);

@override
int crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSimpleGetter(
{required StructWithSimpleSetterTwinNormal that}) {
Expand Down Expand Up @@ -165521,6 +165588,15 @@ class StructWithSimpleSetterTwinNormalImpl extends RustOpaque
.rust_arc_decrement_strong_count_StructWithSimpleSetterTwinNormalPtr,
);

int get something => RustLib.instance.api
.crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalGetSomething(
that: this,
);

void set something(int value) => RustLib.instance.api
.crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSetSomething(
that: this, value: value);

int get simpleGetter => RustLib.instance.api
.crateApiMiscNoTwinExampleAStructWithSimpleSetterTwinNormalSimpleGetter(
that: this,
Expand Down
36 changes: 36 additions & 0 deletions frb_example/pure_dart/lib/src/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45249,6 +45249,22 @@ class RustLibWire implements BaseWire {
_wire__crate__api__misc_no_twin_example_a__StructWithImplBlockInMultiFile_method_in_bPtr
.asFunction<void Function(int, int)>();

WireSyncRust2DartDco
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
int that,
) {
return _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
that,
);
}

late final _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_somethingPtr =
_lookup<ffi.NativeFunction<WireSyncRust2DartDco Function(ffi.UintPtr)>>(
'frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something');
late final _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something =
_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_somethingPtr
.asFunction<WireSyncRust2DartDco Function(int)>();

WireSyncRust2DartDco
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new() {
return _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new();
Expand All @@ -45261,6 +45277,26 @@ class RustLibWire implements BaseWire {
_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_newPtr
.asFunction<WireSyncRust2DartDco Function()>();

WireSyncRust2DartDco
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
int that,
int value,
) {
return _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
that,
value,
);
}

late final _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_somethingPtr =
_lookup<
ffi.NativeFunction<
WireSyncRust2DartDco Function(ffi.UintPtr, ffi.Int32)>>(
'frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something');
late final _wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something =
_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_somethingPtr
.asFunction<WireSyncRust2DartDco Function(int, int)>();

WireSyncRust2DartDco
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_getter(
int that,
Expand Down
22 changes: 22 additions & 0 deletions frb_example/pure_dart/lib/src/rust/frb_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38467,11 +38467,25 @@ class RustLibWire implements BaseWire {
.wire__crate__api__misc_no_twin_example_a__StructWithImplBlockInMultiFile_method_in_b(
port_, that);

dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
Object that) =>
wasmModule
.wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
that);

dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new() =>
wasmModule
.wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new();

dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
Object that, int value) =>
wasmModule
.wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
that, value);

dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_getter(
Object that) =>
Expand Down Expand Up @@ -60440,9 +60454,17 @@ class RustLibWasmModule implements WasmModule {
wire__crate__api__misc_no_twin_example_a__StructWithImplBlockInMultiFile_method_in_b(
NativePortType port_, Object that);

external dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
Object that);

external dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new();

external dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
Object that, int value);

external dynamic /* flutter_rust_bridge::for_generated::WireSyncRust2DartDco */
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_getter(
Object that);
Expand Down
11 changes: 11 additions & 0 deletions frb_example/pure_dart/rust/src/api/misc_no_twin_example_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ impl StructWithSimpleSetterTwinNormal {
pub fn simple_setter(&mut self, value: i32) {
self.0 = value;
}

// should auto strip prefix "get_" and "set_"
#[frb(getter, sync)]
pub fn get_something(&self) -> i32 {
self.0
}

#[frb(setter, sync)]
pub fn set_something(&mut self, value: i32) {
self.0 = value;
}
}

// #1937
Expand Down
19 changes: 19 additions & 0 deletions frb_example/pure_dart/rust/src/frb_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18819,12 +18819,31 @@ pub extern "C" fn frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_ex
)
}

#[no_mangle]
pub extern "C" fn frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something(
that: usize,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_get_something_impl(
that,
)
}

#[no_mangle]
pub extern "C" fn frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new(
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_new_impl()
}

#[no_mangle]
pub extern "C" fn frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something(
that: usize,
value: i32,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_set_something_impl(
that, value,
)
}

#[no_mangle]
pub extern "C" fn frbgen_frb_example_pure_dart_wire__crate__api__misc_no_twin_example_a__StructWithSimpleSetterTwinNormal_simple_getter(
that: usize,
Expand Down
Loading

0 comments on commit 9b49798

Please sign in to comment.