Table of Contents
How to use Rust extension in PHP program, php program rust extension
Home Backend Development PHP Tutorial How to use Rust extension in PHP program, php program rust extension_PHP tutorial

How to use Rust extension in PHP program, php program rust extension_PHP tutorial

Jul 13, 2016 am 09:47 AM
iis php rust

How to use Rust extension in PHP program, php program rust extension

Rust in C or PHP

My basic starting point is to write some compilable Rust code into a library, write some C header files for it, and make an extension in C for the called PHP. It's not easy, but it's fun.
Rust FFI (foreign function interface)

The first thing I did was play around with Rust’s external function interface connecting Rust to C. I once wrote a flexible library using a simple method (hello_from_rust) with a single declaration (a pointer to a C char, otherwise known as a string). Here is the output of "Hello from Rust" after input.

// hello_from_rust.rs
#![crate_type = "staticlib"]
 
#![feature(libc)]
extern crate libc;
use std::ffi::CStr;
 
#[no_mangle]
pub extern "C" fn hello_from_rust(name: *const libc::c_char) {
 let buf_name = unsafe { CStr::from_ptr(name).to_bytes() };
 let str_name = String::from_utf8(buf_name.to_vec()).unwrap();
 let c_name = format!("Hello from Rust, {}", str_name);
 println!("{}", c_name);
}
Copy after login

I split it from a Rust library called from C (or other!). Here's a good explanation of what comes next.

Compiling it will get a file of .a, libhello_from_rust.a. This is a static library that contains all its own dependencies, and we link it when compiling a C program, which allows us to do subsequent things. Note: After we compile, we will get the following output:

note: link against the following native artifacts when linking against this static library
note: the order and any duplication can be significant on some platforms, and so may need to be preserved
note: library: Systemnote: library: pthread
note: library: c
note: library: m
Copy after login

This is what the Rust compiler tells us to link against when we don’t use this dependency.

Call Rust from C

Now that we have a library, we have to do two things to make it callable from C. First, we need to create a C header file for it, hello_from_rust.h. Then link to it when we compile.

The following is the header file:

// hello_from_rust.h
#ifndef __HELLO
#define __HELLO
 
void hello_from_rust(const char *name);
 
#endif
Copy after login

This is a fairly basic header file, just providing a signature/definition for a simple function. Next we need to write a C program and use it.

// hello.c
#include <stdio.h>
#include <stdlib.h>
#include "hello_from_rust.h"
 
int main(int argc, char *argv[]) {
 hello_from_rust("Jared!");
}
Copy after login

We compile it by running this code:

gcc -Wall -o hello_c hello.c -L /Users/jmcfarland/code/rust/php-hello-rust -lhello_from_rust -lSystem -lpthread -lc -lm
Copy after login

Note that the -lSystem -lpthread -lc -lm at the end tells gcc not to link against those "native antiques" so that the Rust compiler can provide them when compiling our Rust library.

By running the following code we can get a binary file:

$ ./hello_c
Hello from Rust, Jared!
Copy after login

Beautiful! We just called the Rust library from C. Now we need to understand how a Rust library gets into a PHP extension.


Call c from php

This part took me some time to figure out, the documentation is not the best in the world for php extensions. The best part is that the php source comes from bundling a script ext_skel (mostly stands for "Extended Skeleton") which generates most of the boilerplate code you need. You can get started by downloading the unquoted php source, writing the code into the php directory and running:

 $ cd ext/
$ ./ext_skel --extname=hello_from_rust

Copy after login

This will generate the basic skeleton needed to create a php extension. Now, move the folders everywhere you want to keep your extensions locally. And move your

  • .rust source
  • .rust library
  • .c header

Enter the same directory. So now you should look at a directory like this:

 .
├── CREDITS
├── EXPERIMENTAL
├── config.m4
├── config.w32
├── hello_from_rust.c
├── hello_from_rust.h
├── hello_from_rust.php
├── hello_from_rust.rs
├── libhello_from_rust.a
├── php_hello_from_rust.h
└── tests
 └── 001.phpt

Copy after login

One directory, 11 files

You can see a good description of these files in the php docs above. Create an extended file. We'll get started by editing config.m4.

Without explanation, here are my results:

PHP_ARG_WITH(hello_from_rust, for hello_from_rust support,
[ --with-hello_from_rust    Include hello_from_rust support])
 
if test "$PHP_HELLO_FROM_RUST" != "no"; then
 PHP_SUBST(HELLO_FROM_RUST_SHARED_LIBADD)
 
 PHP_ADD_LIBRARY_WITH_PATH(hello_from_rust, ., HELLO_FROM_RUST_SHARED_LIBADD)
 
 PHP_NEW_EXTENSION(hello_from_rust, hello_from_rust.c, $ext_shared)
fi
Copy after login

As I understand it, these are basic macro commands. But the documentation on these macros is quite poor (for example: google "PHP_ADD_LIBRARY_WITH_PATH" does not show results written by the PHP team). I came across this PHP_ADD_LIBRARY_PATH macro in a previous thread where someone was talking about linking a static library in a PHP extension. The other recommended macros in the comments were generated after I ran ext_skel.

Now that we have the configuration setup, we need to actually call the library from the PHP script. To do this we have to modify the automatically generated file, hello_from_rust.c. First we add the hello_from_rust.h header file to the include command. Then we need to modify the definition method of confirm_hello_from_rust_compiled.

#include "hello_from_rust.h"
 
// a bunch of comments and code removed...
 
PHP_FUNCTION(confirm_hello_from_rust_compiled)
{
 char *arg = NULL;
 int arg_len, len;
 char *strg;
 
 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  return;
 }
 
 hello_from_rust("Jared (from PHP!!)!");
 
 len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "hello_from_rust", arg);
 RETURN_STRINGL(strg, len, 0);
}
Copy after login

Note: I added hello_from_rust("Jared (fromPHP!!)!");.


Now, we can try to build our extension:

$ phpize
$ ./configure
$ sudo make install

Copy after login

That’s it, generate our meta configuration, run the generated configuration command, and install the extension. When installing, I had to use sudo myself because my user did not own the php extension for the installation directory.

Now, we can run it!

$ php hello_from_rust.php
Functions available in the test extension:
confirm_hello_from_rust_compiled

Hello from Rust, Jared (from PHP!!)!
Congratulations! You have successfully modified ext/hello_from_rust/config.m4. Module hello_from_rust is now compiled into PHP.
Segmentation fault: 11

Copy after login

Not bad, php has entered our c extension, seen our application method list and called it. Next, the c extension has entered our rust library and started printing our string. That's fun! But...what happened to that wrong ending?

As I mentioned, the Rust-related println! macro is used here, but I did not debug it further. If we remove this from our Rust library and return a char* replacement, the segfault disappears.

这里是 Rust 的代码:
复制代码 代码如下:
#![crate_type = "staticlib"]

#![feature(libc)]
extern crate libc;
use std::ffi::{CStr, CString};

#[no_mangle]
pub extern "C" fn hello_from_rust(name: *const libc::c_char) -> *const libc::c_char {
let buf_name = unsafe { CStr::from_ptr(name).to_bytes() };
let str_name = String::from_utf8(buf_name.to_vec()).unwrap();
let c_name = format!("Hello from Rust, {}", str_name);

CString::new(c_name).unwrap().as_ptr()
}

并变更 C 头文件:

#ifndef __HELLO
#define __HELLO
 
const char * hello_from_rust(const char *name);
 
#endif
Copy after login

还要变更 C 扩展文件:

PHP_FUNCTION(confirm_hello_from_rust_compiled)
{
 char *arg = NULL;
 int arg_len, len;
 char *strg;
 
 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
  return;
 }
 
 char *str;
 str = hello_from_rust("Jared (from PHP!!)!");
 printf("%s\n", str);
 
 len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "hello_from_rust", arg);
 RETURN_STRINGL(strg, len, 0);
}
Copy after login

无用的微基准

那么为什么你还要这样做?我还真的没有在现实世界里使用过这个。但是我真的认为斐波那契序列算法就是一个好的例子来说明一个PHP拓展如何很基本。通常是直截了当(在Ruby中):

def fib(at) do
 if (at == 1 || at == 0)
  return at
 else
  return fib(at - 1) + fib(at - 2)
 end
end
Copy after login

而且可以通过不使用递归来改善这不好的性能:

def fib(at) do
 if (at == 1 || at == 0)
  return at
 elsif (val = @cache[at]).present&#63;
  return val 
 end
 
 total = 1
 parent = 1
 gp  = 1
 
 (1..at).each do |i|
  total = parent + gp
  gp  = parent
  parent = total
 end
 
 return total
end
Copy after login
Copy after login

那么我们围绕它来写两个例子,一个在PHP中,一个在Rust中。看看哪个更快。下面是PHP版:

def fib(at) do
 if (at == 1 || at == 0)
  return at
 elsif (val = @cache[at]).present&#63;
  return val 
 end
 
 total = 1
 parent = 1
 gp  = 1
 
 (1..at).each do |i|
  total = parent + gp
  gp  = parent
  parent = total
 end
 
 return total
end
Copy after login
Copy after login

这是它的运行结果:

$ time php php_fib.php
 
real 0m2.046s
user 0m1.823s
sys 0m0.207s
Copy after login

现在我们来做Rust版。下面是库资源:
复制代码 代码如下:
#![crate_type = "staticlib"]

fn fib(at: usize) -> usize {
if at == 0 {
return 0;
} else if at == 1 {
return 1;
}

let mut total = 1;
let mut parent = 1;
let mut gp = 0;
for _ in 1 .. at {
total = parent + gp;
gp = parent;
parent = total;
}

return total;
}

#[no_mangle]
pub extern "C" fn rust_fib(at: usize) -> usize {
fib(at)
}

注意,我编译的库rustc - O rust_lib.rs使编译器优化(因为我们是这里的标准)。这里是C扩展源(相关摘录):

PHP_FUNCTION(confirm_rust_fib_compiled)
{
 long number;
 
 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &number) == FAILURE) {
  return;
 }
 
 RETURN_LONG(rust_fib(number));
}
Copy after login

运行PHP脚本:

<&#63;php
$br = (php_sapi_name() == "cli")&#63; "":"<br>";
 
if(!extension_loaded('rust_fib')) {
 dl('rust_fib.' . PHP_SHLIB_SUFFIX);
}
 
for ($i = 0; $i < 100000; $i ++) {
 confirm_rust_fib_compiled(92);
}
&#63;>
Copy after login

这就是它的运行结果:

$ time php rust_fib.php
 
real 0m0.586s
user 0m0.342s
sys 0m0.221s
Copy after login

你可以看见它比前者快了三倍!完美的Rust微基准!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1026549.htmlTechArticle在PHP程序中使用Rust扩展的方法,php程序rust扩展 C或PHP中的Rust 我的基本出发点就是写一些可以编译的Rust代码到一个库里面,并写为它一些...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

How to open xml format How to open xml format Apr 02, 2025 pm 09:00 PM

Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

See all articles