
该板条箱的目的是为Web API提供生锈的绑定,并允许Rust和JavaScript之间具有高度的互操作性。
由于这些出色的人,该软件被带给您:
谢谢你!
您可以将JavaScript代码直接嵌入Rust:
let message = "Hello, 世界!" ;
let result = js ! {
alert ( @ { message } ) ;
return 2 + 2 * 2 ;
} ;
println ! ( "2 + 2 * 2 = {:?}" , result ) ;还支持关闭:
let print_hello = | name : String | {
println ! ( "Hello, {}!" , name ) ;
} ;
js ! {
var print_hello = @ { print_hello } ;
print_hello ( "Bob" ) ;
print_hello . drop ( ) ; // Necessary to clean up the closure on Rust's side.
}您也可以通过Serde通过任意结构:
# [ derive ( Serialize ) ]
struct Person {
name : String ,
age : i32
}
js_serializable ! ( Person ) ;
js ! {
var person = @ { person } ;
console . log ( person . name + " is " + person . age + " years old." ) ;
} ;该板条箱还暴露了许多Web API,例如:
let button = document ( ) . query_selector ( "#hide-button" ) . unwrap ( ) . unwrap ( ) ;
button . add_event_listener ( move |_ : ClickEvent | {
for anchor in document ( ) . query_selector_all ( "#main a" ) {
js ! ( @ { anchor } . style = "display: none;" ; ) ;
}
} ) ;也支持将Rust功能暴露于JavaScript:
# [ js_export ]
fn hash ( string : String ) -> String {
let mut hasher = Sha1 :: new ( ) ;
hasher . update ( string . as_bytes ( ) ) ;
hasher . digest ( ) . to_string ( )
}然后,您可以从Node.js:
var hasher = require ( "hasher.js" ) ; // Where `hasher.js` is generated from Rust code.
console . log ( hasher . hash ( "Hello world!" ) ) ;或者,您可以使用相同的.js文件并在Web浏览器中使用它:
< script src =" hasher.js " > </ script >
< script >
Rust . hasher . then ( function ( hasher ) {
console . log ( hasher . hash ( "Hello world!" ) ) ;
} ) ;
</ script >如果您使用的是包裹,也可以使用我们的实验包裹插件;首先在您现有的包裹项目中执行此操作:
$ npm install --save parcel-plugin-cargo-web
然后简单:
import hasher from "./hasher/Cargo.toml" ;
console . log ( hasher . hash ( "Hello world!" ) ) ; 看看一些示例:
examples/minimal - 一个完全最小的示例,调用警报examples/todomvc一种天真的todomvc应用程序;显示如何致电DOMexamples/hasher显示如何将Rust功能导出到JavaScript以及如何从Vanilla Web浏览器环境或Nodejs拨打它们examples/hasher-parcel显示如何在包裹项目中导入和调用出口的Rust功能pinky-web NES仿真器;您可以在此处使用预编译版本API文档也可以供您查看。
安装货物 - 韦布:
$ cargo install -f cargo-web
进入examples/todomvc ,并使用以下命令之一启动示例:
使用Rust的本机WebAssembly后端编译到WebAssmbly:
$ cargo web start --target=wasm32-unknown-unknown
使用Emscripten编译为ASM.JS:
$ cargo web start --target=asmjs-unknown-emscripten
使用Emscripten编译到WebAssembly:
$ cargo web start --target=wasm32-unknown-emscripten
请访问http://localhost:8000 with浏览器。
对于*-emscripten而言,无需cargo-web ,但是不需要emscripten的本地wasm32-unknown-unknown不需要货物,需要cargo-web工作!
stdweb 0.4.20
wasm-bindgen兼容性FullscreenChangeEvent stdweb 0.4.19
Document::fullscreen_enabledDocument::fullscreen_elementInputElement::selection_startInputElement::selection_endInputElement::set_selection_startInputElement::set_selection_endObject::to_iterWindow::confirm&Array现在可以通过TryFrom转换为Vec sfutures相关的不稳定的API已更新以与最新的夜间合作syn依赖项已更新为版本1 stdweb 0.4.18
js!现在可以在use中导入宏BeforeUnloadEventUnloadEventIBlob::sliceIBlob::slice_with_content_typeIWindowOrWorker::set_clearable_timeout stdweb 0.4.17
futures相关的不稳定的API已更新以与最新的夜间合作stdweb 0.4.16
wasm-bindgen兼容性;您现在可以使用wasm-bindgen在项目中使用stdwebcargo-web版本现在为0.6.24 stdweb 0.4.15
futures相关的不稳定的API已更新以与最新的夜间合作FormDataFormDataEntryMouseButtonsStateBlob::new stdweb 0.4.14
js!如果您没有从JS片段中返回任何内容,则宏现在会生成更有效的代码。这使得在绝大多数情况下不必添加@(no_return)注释。File stdweb 0.4.13
js!宏的空间处理ITouchEventTouchTouchTypeTouchEventTouchMoveTouchLeaveTouchEnterTouchEndTouchCancelTouchStartXmlHttpRequest::set_response_type stdweb 0.4.12
wasm32-unknown-unknown目标而没有cargo-web改进了诊断方法stdweb 0.4.11
cargo-web版本现在为0.6.22wasm32-unknown-unknown现已正式支持稳定的Rustwasm32-unknown-unknown为基础的调试js!现在使用程序宏部分实现宏Document::import_nodeIElement::slotIElement::attach_shadowIElement::shadow_rootISlotableShadowRootShadowRootModeTemplateElementSlotElementSlotChangeEventIParentNode::query_selector和IParentNode::query_selector_all现在返回正确的错误类型stdweb 0.4.10 , stdweb-derive 0.5.1
IElement::insert_adjacent_htmlIElement::insert_html_beforeIElement::insert_html_afterIElement::prepend_htmlIElement::append_htmlIElement::namespace_uriIElement::closestDocument::create_element_nsWindow::get_selectionAbortErrorSelectionTypeSelectionRange.try_into() / .try_from() )现在可转换为TypeErrorDrawImageError ) js!宏TypeError (被错误地视为DOMException ).into() / .from()将Number转换为f64Mut ,这是一种新的包装类型,可安全地将FnMut封闭插入js!宏;目前是可选的,但是这种包装纸的使用将来将是强制性的!FnMut关闭不能再递归地称为#[derive(ReferenceType)]现在支持一个有限的通用类型子集#[async_test]属性宏支持异步单元测试(仅每晚)futures 0.3 (仅每晚) stdweb 0.4.9 , stdweb-derive 0.5.0
ScrollEventDragRelatedEventDragEventDragStartEventDragEndEventDragEnterEventDragLeaveEventDragOverEventDragExitEventDragDropEventDataTransferEffectAllowedDropEffectDataTransferItemListDataTransferItemDataTransferItemKindIDragEventValue S现在可以转换为try_into Option< Serde< T > >i32和f64以外的其他类型keyup事件中进行KeyDownEvent化,因为仅检查了事件的JS类型,并且keyup和keydown共享相同的JS类型( KeyboardEvent )。从现在开始,还检查了事件的type字段,因此不再允许这样的转换。 0.4.8
wasm32-unknown-unknown最新夜间汇编PointerLockChangeEventPointerLockErrorEventMouseWheelEventMouseWheelDeltaModeXhrResponseTypeXmlHttpRequest::raw_responseWindow::device_pixel_ratioDocument::pointer_lock_elementDocument::exit_pointer_lock 0.4.7
AuxClickEventMouseEnterEventMouseLeaveEventContextMenuEventSelectionChangeEventFileListFileReaderReadyStateGamepadGamepadButtonGamepadButtonMappingGamepadEventCanvasRenderingContext2d::clear_rectVec S和ArrayBuffer s创建TypedArray时的泄漏。 0.4.6
docs.rsSubmitEventIChildNodeCanvasElement::to_data_url 0.4.5
DocumentFragmentSelectElementOptionElementHtmlCollectionNode::from_htmlValue::is_nullSocketMessageDataNodeTypefutures 0.2 0.4.4
docs.rs (希望)。Location::originLocation::protocolLocation::hostLocation::hostnameLocation::portLocation::pathnameLocation::searchSecurityError :Location::hashLocation::href 0.4.3
WeakMap中键的对象(例如,在Firefox下的一些与WebGL相关的对象)Element::get_bounding_client_rectElement::scroll_topElement::scroll_leftWindow::page_x_offsetWindow::page_y_offsetNodeList::itemDocument::bodyDocument::headDocument::titleDocument::set_titleIMouseEvent::offset_xIMouseEvent::offset_yCompositeOperationLineCapLineJoinRepetitionTextAlignTextBaselineAddColorStopError , DrawImageError , GetImageDataErrorMouseOverEventMouseOutEventPointerOverEventPointerEnterEventPointerDownEventPointerMoveEventPointerUpEventPointerCancelEventPointerOutEventPointerLeaveEventGotPointerCaptureEventLostPointerCaptureEventIPointerEvent 0.4.2
CanvasRenderingContext2d::get_canvasFillRule和SocketReadyStateIElement新属性与属性相关的方法Date绑定0.4.1
wasm32-unknown-unknown最新夜间生锈SocketBinaryType枚举CanvasRenderingContext2d的许多新方法CanvasGradient , CanvasPattern , CanvasStyle , ImageData , TextMetricsIndexSizeError , NotSupportedError , TypeError 0.4
Value中删除了Array和Object变体;这些现在被视为ReferenceValue具有额外的变体: SymbolInputElement::set_kindInputElement::filesKeydownEvent > KeyDownEventKeyupEvent > KeyUpEventKeypressEvent > KeyPressEventReadyState > FileReaderReadyStateInputElement::value > InputElement::raw_valueInputElement::set_value > InputElement::set_raw_valueArrayBuffer::new现在采用u64参数InputElement::set_raw_value现在使用&str而Into< Value >usize的每种方法现在返回u32INode::remove_child现在在Ok情况下返回Nodeu64 :ArrayBuffer::leni32而不是f64 :IMouseEvent::client_xIMouseEvent::client_yIMouseEvent::movement_xIMouseEvent::movement_yIMouseEvent::screen_xIMouseEvent::screen_yResult :INode::insert_beforeINode::replace_childINode::clone_nodeStringMap::insertTokenList::addTokenList::removeDocument::create_elementIEventTarget::dispatch_eventFileReader::read_as_textFileReader::read_as_array_bufferFileReader::read_as_textHistory::replace_stateHistory::goHistory::backHistory::forwardLocation::hrefLocation::hashCanvasElement::to_data_urlCanvasElement::to_blobArrayBuffer::newINode::base_uri现在返回String ,而不是Option< String >InputElement::raw_value现在返回String而不是ValueINode::inner_text移动到IHtmlElement::inner_textDocument::query_selector和Document::query_selector_all移动到IParentNodeIElement::query_selector IParentNode IElement::query_selector_all )Document::get_element_by_id已移至INonElementParentNodeTryFrom / TryInto在任意参考的对象之间转换的毯子式cargo-web构建时,不必再调用stdweb::initialize stdweb::event_loopwasm32-unknown-unknown cdylib板条板上的支持XmlHttpRequestWebSocketMutationObserverHistoryTextAreaElementCanvasElementMouseDownEventMouseUpEventMouseMoveEventPopStateEventResizeEventReadyStateChangeSocketCloseEventSocketErrorEventSocketOpenEventSocketMessageEventReferenceType和InstanceOfstdweb-derive板条箱中添加#[derive(ReferenceType)] ;现在可以在stdweb之外定义自定义API绑定#[js_export]程序属性(仅wasm32-unknown-unknown )DomException和子类型,以使JavaScript异常传递IElement现在从INode继承ReferenceType继承stdweb::traits模块充当use前奏 - 我们所有的界面特征console!宏PartialEq和Eq 0.3
ErrorEvent方法LoadEvent > ResourceLoadEventAbortEvent > ResourceAbortEventErrorEvent > ResourceErrorEventUnsafeTypedArray js!Once以将FnOnce关闭到js! 根据任何一个
可以选择。
来自Mozilla开发人员网络的文档片段涵盖了CC-BY-SA版本2.5或更高版本。
参见贡献