Giter Club home page Giter Club logo

Comments (18)

ying32 avatar ying32 commented on June 6, 2024

😟😟😟我刚也遇到你这个问题。。。。 感觉Rust中不好弄。。。

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

这样是否可以

#![feature(trait_alias)]

pub trait TNotifyEvent = Fn(i32) -> i32;

fn test(f: impl TNotifyEvent) {
    println!("{}", f(10));
}

fn callback(i: i32) -> i32 {
    i * 10
}

let x = 10;
test(MainForm::callback);
test(|i:i32| i + x);

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

这个得要看lclapi.rsdoEventCallback事件中能不能更好的处理这种了

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

我试了下, 貌似不好实现,而且我现在这个版本貌似开启了

#![feature(trait_alias)]

也不能编译通过。

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

确实 我试过,

#[link(name = "liblcl")]
extern "system" {
    // ...
    pub fn MainMenu_SetOnChange(AObj: usize, AEventId: TMenuChangeEvent);
    // ...
}

impl trait不能往里传。

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

试了很多种方法都无解,分析了下汇编代码,
像这种的:

TMainForm::test(testEvent);

在汇编中生成了一个拷贝版:

lea     rcx, [rbp+var_20] ; void (__fastcall *__ptr32 *)(int)
mov     edx, 0Ah        ; int
call    _ZN4core3ops8function2Fn4call17h31c4630f33dae3cfE ; core::ops::function::Fn::call::h31c4630f33dae3cf
jmp     short $+2

_ZN4core3ops8function2Fn4call17h31c4630f33dae3cfE这个实际内部直接调用的原来的。。。。

sub     rsp, 38h
mov     [rsp+38h+var_C], edx
mov     [rsp+38h+var_8], rcx
mov     ecx, [rsp+38h+var_C] ; int
call    _ZN7apptest9testEvent17hb62bbdb09a071e27E ; apptest::testEvent::hb62bbdb09a071e27
nop
add     rsp, 38h
retn

_ZN7apptest9testEvent17hb62bbdb09a071e27E实际上就是testEvent

最终他的调就大概就是这样:

 
 f: impl TNotifyEvent ->  _ZN4core3ops8function2Fn4call17h31c4630f33dae3cfE(&f, 10); -> testEvent(10);
 

PS:要实现估计得另想办法了

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

做了种实验性的,虽然没有实现你说的那种,但是现在是用的下面的这样的:

    fn onBtnColorDialogClick(&self, _sender: usize) {
        if self.dlgColor.Execute() {
            let color = self.dlgColor.Color();
            println!("color={}", color);
            self.form.SetColor(color);
        }
    }

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

好的,麻烦你了,我把govcl的examples写一遍学习学习。

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

ImplISId是不是用用derive更好点。

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

是的,刚写好这个

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

不过你要拿rust把govcl的例子写一遍感觉很累的。。。

新的Form定义现在是这样:

#[derive(VclForm)]
pub struct TForm2 {
    btn: TButton,
    pub form: TForm, // 固定名form, 放最后,前面引用完后,后面move到form。
}

App的:

#[derive(VclApp)]
struct TApp {
    mainForm: TMainForm,
    form2: TForm2,
}

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

感谢,没什么,学习一下

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

Set系列函数是否可以返回self让其支持链式调用:

form.SetWidth(800).SetHeight(600);

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

有个问题,比如:

fn Test1(&self) -> &Self {
    // 这地方应该是返回 self还是&self呢,,我测试好像都没问题  
    self     
    &self  
}

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

应该是rust特有的吧,我也不太了解,我试着&&self一样可以,这里我觉得用self就可以了。

我查了下可能是编译器优化掉了,参考链接:
https://users.rust-lang.org/t/reference-to-a-reference/44753

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

查了下,用self就行了:

    pub fn init(&self) -> &Self {
        let sid = self.getSId();

        // TForm
        self.form
            .SetCaption("你好,Rust! - Hello Rust!")
            .SetWidth(200)
            .SetHeight(300)
            .EnabledMaximize(false)
            .SetPosition(TPosition::poScreenCenter);

        // TButton
        self.btn.SetParent(self)
            .SetCaption("msgbox")
            .SetOnClick(sid, Self::onBtnClick)
            .AnchorHorizontalCenterTo(self)
            .AnchorVerticalCenterTo(self);

        return self;
    }

from rust-vcl.

bstaint avatar bstaint commented on June 6, 2024

好的 谢谢了 我先关闭了 有问题再来请教。

from rust-vcl.

ying32 avatar ying32 commented on June 6, 2024

好的,请教不敢当,一起讨论讨论,我学Rust也就几天时间而已。自己都还是一知半解的😂😂😂

看了下汇编代码,&self和 self是一样的,编译器会优化掉:

pub fn Test1(&self) -> &Self {
    &self
}
mov rax, [rsp+38h+var_10]  ; return &self;

pub fn Test2(&self) -> &Self {
    self
}
mov rax, [rsp+48h+var_28] ; return self;

from rust-vcl.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.