
Creating a PHP extension in Rust - jaredonline
http://jaredonline.svbtle.com/creating-a-php-extension-in-rust
======
tveita
So who is deallocating the returned string in
confirm_hello_from_rust_compiled? Looks like you lost a fair bit of the safety
checking in the Rust to C transition, if you didn't even have to mark the
whole function unsafe.

~~~
masklinn
That's not how unsafe works. Marking a function as unsafe is more or less
solely a decision of the developer (though foreign functions are implicitly
unsafe), and an unsafe block does not taint the enclosing function or it
wouldn't be possible to build safe interface from unsafe building blocks,
making the whole thing serve a completely different purpose.

------
decasteve
I've also toyed with a similar idea. I'm not a huge fan of writing anything
"significant" in PHP but it does make html templating convenient. I would
rather write the bulk of my code in a language like C++ or Rust, and then pass
off to PHP to do the html (or json or xml) view.

~~~
smt88
I used to think PHP was good for templating, but then I learned about code-
less templating engines like Jinja, and I'll never go back.

When you use a templating engine, you can change the backend entirely without
messing with the template. You can also compile/optimize the display of data
in the template more easily.

Some awesome person has made a Jinja2 lib that comes with Rust bindings, which
looks cool (though I haven't tried it yet):
[https://github.com/jroweboy/jinja2-c](https://github.com/jroweboy/jinja2-c)

~~~
Mahn
> When you use a templating engine, you can change the backend entirely
> without messing with the template. You can also compile/optimize the display
> of data in the template more easily.

To be fair, all that you can do with a templating engine you can do "natively"
as well. You _can_ for example have code-less templates in PHP if you are
disciplined enough to keep it that way. The only thing template engines do is
enforce not being able to shoot yourself in the feet, but it's not an absolute
must if you know what you are doing.

~~~
smt88
Sorry, should have clarified "code-less". I definitely don't like logic in
templates (although React is making me re-think that as a strict policy). But
I actually meant _code_ as in _any_ code. A line in PHP might be this:

    
    
        <a href="<?php echo $some_url ?>">Some link</a>
    

So that "echo" is the code. And the same thing in Jinja is this:

    
    
        <a href="{{ some_url }}">Some link</a>
    

In Jinja, it's _only_ HTML + the names of variables (which I guess you could
call "data"). I can now take that line and use it with any language that
supports Jinja-style syntax (PHP, Node, Python, etc.) It's also not unlikely
that a designer will know it.

Also, sometimes when you use PHP, you have to sacrifice correctness or speed
for readability. You don't have that problem when you're running templates
that were compiled to PHP, rather than hand-written PHP.

Finally, template inheritance is absolutely _amazing_. Can't live without it
now.

~~~
Mahn
For the record, your first example can be written in this manner with short
tags on:

    
    
        <a href="<?=$some_url?>">Some link</a>
    

Which is effectively pretty close to a templating pseudo-syntax.

~~~
smt88
That's a perfect example of a bad practice that's done for readability in PHP
templating.

Short tags are off by default in PHP (and have been for years), so you're
losing some portability when you use them in your code. If, for example,
someone reinstalls your PHP package and overwrites your config (which is very
easy to do), your app suddenly breaks.

------
duveit
Nice job!

