Skip to content

Conversation

Xenira
Copy link
Collaborator

@Xenira Xenira commented Jul 13, 2025

Refs: #506

Description

Allow multiple refs with lifetime. e.g:

    pub fn self_multi_ref<'a>(
        self_: &'a mut ZendClassObject<TestClass>,
        val: &str,
    ) -> &'a mut ZendClassObject<TestClass> {
        self_.string = format!("Changed to {val}");
        self_
    }

Need to think this through though.

Checklist

Check the boxes that apply (put an x in the brackets, like [x]). You can also check boxes after the PR is created.

❤️ Thank you for your contribution!

@coveralls
Copy link

coveralls commented Jul 13, 2025

Pull Request Test Coverage Report for Build 16248137065

Details

  • 0 of 2 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.006%) to 22.325%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/macros/src/function.rs 0 2 0.0%
Totals Coverage Status
Change from base Build 16247613957: -0.006%
Covered Lines: 870
Relevant Lines: 3897

💛 - Coveralls

@Xenira Xenira force-pushed the 506-missing-lifetime-specifier-when-taking-self_-and-zval-and-returning-self_ branch from fe24877 to f815a13 Compare July 13, 2025 10:27
@Xenira
Copy link
Collaborator Author

Xenira commented Jul 13, 2025

@ptondereau do you see any reason why we sholdn't do this. Seems to be working and I see no unsafe code that could cause trouble here.

@Xenira Xenira requested a review from ptondereau July 13, 2025 18:23
@Xenira
Copy link
Collaborator Author

Xenira commented Jul 13, 2025

Relevant generated code:

::ext_php_rs::builders::FunctionBuilder::new(
                                    "selfMultiRef",
                                    {
                                        extern "C" fn handler(
                                            ex: &mut ::ext_php_rs::zend::ExecuteData,
                                            retval: &mut ::ext_php_rs::types::Zval,
                                        ) {
                                            use ::ext_php_rs::convert::IntoZval;
                                            let mut val = ::ext_php_rs::args::Arg::new(
                                                "val",
                                                <&str as ::ext_php_rs::convert::FromZvalMut>::TYPE,
                                            );
                                            let result = {
                                                let (parse, this) = ex.parser_method::<TestClass>();
                                                let this = match this {
                                                    Some(this) => this,
                                                    None => {
                                                        ::ext_php_rs::exception::PhpException::default(
                                                                "Failed to retrieve reference to `$this`".into(),
                                                            )
                                                            .throw()
                                                            .unwrap();
                                                        return;
                                                    }
                                                };
                                                let parse_result = parse
                                                    .arg(&mut val)
                                                    .not_required()
                                                    .parse();
                                                if parse_result.is_err() {
                                                    return;
                                                }
                                                TestClass::self_multi_ref(
                                                    this,
                                                    {
                                                        match val.val() {
                                                            Some(val) => val,
                                                            None => {
                                                                ::ext_php_rs::exception::PhpException::default(
                                                                        "Invalid value given for argument `val`.".into(),
                                                                    )
                                                                    .throw()
                                                                    .expect("Failed to throw PHP exception.");
                                                                return;
                                                            }
                                                        }
                                                    },
                                                )
                                            };
                                            if let Err(e) = result.set_zval(retval, false) {
                                                let e: ::ext_php_rs::exception::PhpException = e.into();
                                                e.throw().expect("Failed to throw PHP exception.");
                                            }
                                        }
                                        handler
                                    },
                                )
                                .arg(
                                    ::ext_php_rs::args::Arg::new(
                                        "val",
                                        <&str as ::ext_php_rs::convert::FromZvalMut>::TYPE,
                                    ),
                                )
                                .not_required()
                                .returns(
                                    <&mut ZendClassObject< // <-- Lifetime dropped here
                                        TestClass,
                                    > as ::ext_php_rs::convert::IntoZval>::TYPE,
                                    false,
                                    <&mut ZendClassObject< // <-- And here
                                        TestClass,
                                    > as ::ext_php_rs::convert::IntoZval>::NULLABLE,
                                ),

Copy link
Collaborator

@ptondereau ptondereau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! To me, this is great!

@Xenira Xenira merged commit 0b0368a into master Jul 13, 2025
57 checks passed
@Xenira Xenira deleted the 506-missing-lifetime-specifier-when-taking-self_-and-zval-and-returning-self_ branch July 13, 2025 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

missing lifetime specifier when taking self_ and &Zval and returning self_

3 participants