// 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);
}
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
// hello_from_rust.h #ifndef __HELLO #define __HELLO void hello_from_rust(const char *name); #endif
// hello.c
#include <stdio.h>
#include <stdlib.h>
#include "hello_from_rust.h"
int main(int argc, char *argv[]) {
hello_from_rust("Jared!");
}
gcc -Wall -o hello_c hello.c -L /Users/jmcfarland/code/rust/php-hello-rust -lhello_from_rust -lSystem -lpthread -lc -lm
$ ./hello_c Hello from Rust, Jared!
$ cd ext/ $ ./ext_skel --extname=hello_from_rust
. ├── 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
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
#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);
}
$ phpize $ ./configure $ sudo make install
$ 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
#ifndef __HELLO #define __HELLO const char * hello_from_rust(const char *name); #endif
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);
}
def fib(at) do if (at == 1 || at == 0) return at else return fib(at - 1) + fib(at - 2) end end
def fib(at) do if (at == 1 || at == 0) return at elsif (val = @cache[at]).present? 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
def fib(at) do if (at == 1 || at == 0) return at elsif (val = @cache[at]).present? 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
$ time php php_fib.php real 0m2.046s user 0m1.823s sys 0m0.207s
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));
}
<?php
$br = (php_sapi_name() == "cli")? "":"<br>";
if(!extension_loaded('rust_fib')) {
dl('rust_fib.' . PHP_SHLIB_SUFFIX);
}
for ($i = 0; $i < 100000; $i ++) {
confirm_rust_fib_compiled(92);
}
?>
$ time php rust_fib.php real 0m0.586s user 0m0.342s sys 0m0.221s
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有