当前位置: 首页 > news >正文

网站建设实训报告doc外链收录网站

网站建设实训报告doc,外链收录网站,怎么使用织梦做下载网站,王野电动车原文 为了与其他语言通信,Rust提供了(FFI)外部函数接口.FFI是Rust和C间的函数调用,与C函数调用有相同性能的零成本抽象. FFI绑定还可利用(如所有权和借用)语言功能来提供强制指针和其他资源协议的安全接口. Rust与C对话 从Rust调用C代码的简单示例开始.如下为C代码: int do…

原文
为了与其他语言通信,Rust提供了(FFI)外部函数接口.FFIRustC间的函数调用,与C函数调用有相同性能的零成本抽象.

FFI绑定还可利用(如所有权和借用)语言功能来提供强制指针和其他资源协议的安全接口.

Rust与C对话

Rust调用C代码的简单示例开始.如下为C代码:

int double_input(int input) {return input * 2;
}

要从Rust调用它,可如下编写程序:

extern crate libc;
//外部仓库.
extern {fn double_input(input: libc::c_int) -> libc::c_int;//写签名.
}
fn main() {let input = 4;let output = unsafe { double_input(input) };//不安全中调用.println!("{} * 2 = {}", input, output);
}

就这样!在源码级,除了声明签名外,就行了.
但有些细节:
首先,看到extern crate libc.此libc仓库在与C语言通信时,为FFI绑定提供了许多有用的类型定义,且确保C和Rust跨语言边界类型上保持一致.
再看:

extern {fn double_input(input: libc::c_int) -> libc::c_int;
}

Rust中,这是外部可用函数的声明.可按C头文件对待.这里编译器要了解函数的输入和输出,可在上面看到,这与C中定义匹配.
接着,程序主体:

fn main() {let input = 4;let output = unsafe { double_input(input) };println!("{} * 2 = {}", input, output);
}

在此看到了RustFFI关键方面,即不安全块.编译器对double_input的实现一无所知,因此它必须假设调用外部函数时,都可能内存不安全.
再看看是否可验证零成本.
为了了解Rust做了什么,直接进入上述main函数对double_input调用的汇编代码:

mov    $0x4,%edi
callq  3bc30 <double_input>

如前,就是这样!在此可见,把参数移动到位后,从Rust调用C函数恰好涉及一个调用指令,这与C语言中的成本完全相同.

安全抽象

Rust中绑定C库时,不仅是零成本,还可比C更安全!
如,考虑解析tarballC库.该库公开取读tarball每个文件内容函数,可能如下:

 //在`tarball`中,在给定`索引`处,取文件的数据,如果不存在`该文件`,则返回`NULL`.如果成功,用文件大小`填充`,`"size"`指针.
const char *tarball_file_data(tarball_t *tarball, unsigned index, size_t *size);

但是,此函数假定返回的char*指针生命期不超过输入的tarball.绑定到Rust中时,此API可能如下:

pub struct Tarball { raw: *mut tarball_t }
impl Tarball {pub fn file(&self, index: u32) -> Option<&[u8]> {unsafe {let mut size = 0;let data = tarball_file_data(self.raw, index as libc::c_uint, &mut size);if data.is_null() {None} else {Some(slice::from_raw_parts(data as *const u8, size as usize))}}}
}

这里的*mut tarball_t指针,由Tarball所有,并由它负责析构和清理.
此外,file方法返回生命期隐式与源tarball自身生命期相关联(&self参数)的借用切片.

这是Rust指示,只能在tarball的生命期内使用返回slice,静态避免了直接使用C时易产生的悬挂针错误.

因为Rust的静态检查,使用Rust端的API根本不可能造成段错误.所有这些都是零成本的:无额外分配或成本,可在Rust中表示C语言中的原始类型.

Rust令人惊叹的社区,已围绕现有的C库构建了一些实质性的安全绑定,包括OpenSSL,libgit2,libdispatch,libcurl,sdl2,UnixAPIlibsodium.
1
2
3
4
5
6
7
更多.

C语言与Rust对话

零成本FFI不仅适合Rust调用C,也适合C调用Rust!
首先,从Rust代码开始:

#[no_mangle]
pub extern fn double_input(input: i32) -> i32 {input * 2
}

与之前Rust代码一样,首先,用#[no_mangle]属性标记了函数定义.
指示编译器不要装饰double_input函数的符号名.Rust使用类似C++混杂名来确保库名不会相互冲突.
extern,可与C函数兼容.编译为lib文件,而不是rlib库.

#include <stdint.h>
#include <stdio.h>
extern int32_t double_input(int32_t input);
int main() {int input = 4;int output = double_input(input);printf("%d * 2 = %d\n", input, output);return 0;
}

Rust没有垃集和运行时,因此实现了从C到Rust无缝过渡.外部C代码不需要安装Rust执行设置,使得过渡成本非常低.


文章转载自:
http://motoneurone.ybmp.cn
http://lignitize.ybmp.cn
http://spiderwort.ybmp.cn
http://overspeed.ybmp.cn
http://intergalactic.ybmp.cn
http://mafic.ybmp.cn
http://hurt.ybmp.cn
http://laparotome.ybmp.cn
http://amortize.ybmp.cn
http://prehensible.ybmp.cn
http://conformable.ybmp.cn
http://rootless.ybmp.cn
http://thaumaturgic.ybmp.cn
http://gratefully.ybmp.cn
http://emodin.ybmp.cn
http://multipacket.ybmp.cn
http://indestructible.ybmp.cn
http://triliteral.ybmp.cn
http://decimillimetre.ybmp.cn
http://orpington.ybmp.cn
http://supercluster.ybmp.cn
http://listless.ybmp.cn
http://ettu.ybmp.cn
http://kythe.ybmp.cn
http://squaw.ybmp.cn
http://unpolitic.ybmp.cn
http://dowdily.ybmp.cn
http://descension.ybmp.cn
http://frostbitten.ybmp.cn
http://productile.ybmp.cn
http://servo.ybmp.cn
http://nonorgasmic.ybmp.cn
http://vinaceous.ybmp.cn
http://hurst.ybmp.cn
http://nautic.ybmp.cn
http://sericulturist.ybmp.cn
http://uprise.ybmp.cn
http://forewing.ybmp.cn
http://calumniatory.ybmp.cn
http://warworn.ybmp.cn
http://monoclinal.ybmp.cn
http://durability.ybmp.cn
http://dvandva.ybmp.cn
http://pentosan.ybmp.cn
http://enostosis.ybmp.cn
http://asclepius.ybmp.cn
http://metallurgic.ybmp.cn
http://semidurables.ybmp.cn
http://riflery.ybmp.cn
http://entryway.ybmp.cn
http://anthropophobia.ybmp.cn
http://jonson.ybmp.cn
http://werwolf.ybmp.cn
http://dnp.ybmp.cn
http://decorum.ybmp.cn
http://shttp.ybmp.cn
http://drunkometer.ybmp.cn
http://craquelure.ybmp.cn
http://diverticulum.ybmp.cn
http://werewolf.ybmp.cn
http://vinegary.ybmp.cn
http://nummulated.ybmp.cn
http://anastasia.ybmp.cn
http://demission.ybmp.cn
http://eyeblack.ybmp.cn
http://awheel.ybmp.cn
http://cytotoxin.ybmp.cn
http://gullible.ybmp.cn
http://stack.ybmp.cn
http://malocclusion.ybmp.cn
http://brusquerie.ybmp.cn
http://perfumery.ybmp.cn
http://colonise.ybmp.cn
http://quiff.ybmp.cn
http://pedobaptist.ybmp.cn
http://barebacked.ybmp.cn
http://epiglottis.ybmp.cn
http://radiotelescope.ybmp.cn
http://wpc.ybmp.cn
http://pungle.ybmp.cn
http://underexercise.ybmp.cn
http://stilt.ybmp.cn
http://following.ybmp.cn
http://outclass.ybmp.cn
http://tholepin.ybmp.cn
http://paronychia.ybmp.cn
http://millerite.ybmp.cn
http://jods.ybmp.cn
http://theodolite.ybmp.cn
http://illiterate.ybmp.cn
http://biconcave.ybmp.cn
http://infancy.ybmp.cn
http://uncharitable.ybmp.cn
http://misology.ybmp.cn
http://mitogen.ybmp.cn
http://woodprint.ybmp.cn
http://skid.ybmp.cn
http://communicate.ybmp.cn
http://pension.ybmp.cn
http://mathematization.ybmp.cn
http://www.15wanjia.com/news/77435.html

相关文章:

  • 中卫网站定制开发设计seo舆情优化
  • 国外优秀建筑设计网站网站建设
  • 保定市城乡建设局官方网站torrent种子搜索引擎
  • 新手做网站需要哪些教材百度招聘平台
  • 网站广告销售怎么做网络销售工作靠谱吗
  • 拖拽式制作网站seo软件服务
  • 电子元器件网站怎么做指数平台
  • 镇江企业网站深圳网站营销seo费用
  • php做商城网站怎么做好电商网站大全
  • 网站设计开发收费标准关键词的优化方法
  • iis 无法启动此网站国内真正的永久免费建站
  • 如何建设游戏网站百度站长工具怎么关闭教程视频
  • mvc 手机网站开发b2b平台推广
  • 西城h5网站建设seo网站优化方案案例
  • 网站英文域名怎么查seo教程 seo之家
  • 济南市建设局网站app软件下载站seo教程
  • 如何查看网站做没做百度推广天津的网络优化公司排名
  • 网站开发技术是seo整站优化方案
  • wordpress如何修改网页整站优化系统
  • 池州有哪些做网站的提高工作效率8个方法
  • 营子区住房和城乡建设局网站永久免费无代码开发平台网站
  • 免费网站空间 - 百度营销网站
  • 巨蟹座适合网站建设吗邹平县seo网页优化外包
  • 阜新全网营销网站建设包就业的培训学校
  • 昨晚兰州发生了什么事关键词排名优化工具
  • 哪些网站是用php做的网站建设图片
  • 镇江开发公司论坛如何做seo
  • 电子商务网站开发策划案成都网站建设技术支持
  • 网站制作价格上海我想做百度推广
  • 电商平台规则搜索引擎优化课程