wasm_encoder

Struct BranchHints

Source
pub struct BranchHints { /* private fields */ }
Expand description

Helper structure to encode the metadata.code.branch_hint custom section.

This section was defined in the branch-hinting proposal for WebAssembly: https://github.com/WebAssembly/branch-hinting.

§Example

use wasm_encoder::*;

let mut module = Module::new();

let mut types = TypeSection::new();
types.ty().function([], []);
module.section(&types);

let mut funcs = FunctionSection::new();
funcs.function(0);
module.section(&funcs);

let mut code = CodeSection::new();
let mut body = Function::new([]);

body.instruction(&Instruction::I32Const(1));
let if_offset = body.byte_len();
body.instruction(&Instruction::If(BlockType::Empty));
body.instruction(&Instruction::End);
body.instruction(&Instruction::End);
code.function(&body);

let mut hints = BranchHints::new();
hints.function_hints(0, [BranchHint {
    branch_func_offset: if_offset as u32,
    branch_hint_value: 1, // taken
}]);
module.section(&hints);
module.section(&code);

let wasm = module.finish();
let wat = wasmprinter::print_bytes(&wasm).unwrap();
assert_eq!(wat, r#"(module
  (type (;0;) (func))
  (func (;0;) (type 0)
    i32.const 1
    (@metadata.code.branch_hint "\01")
    if ;; label = @1
    end
  )
)
"#);

Implementations§

Source§

impl BranchHints

Source

pub fn new() -> Self

Construct an empty encoder for the branch hints custom section.

Source

pub fn function_hints<I>(&mut self, func: u32, hints: I)

Adds a new set of function hints for the func specified.

Source

pub fn is_empty(&self) -> bool

Returns if this is an empty section.

Source

pub fn len(&self) -> u32

Returns the number of functions that have hints registered in this sectino.

Trait Implementations§

Source§

impl Debug for BranchHints

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BranchHints

Source§

fn default() -> BranchHints

Returns the “default value” for a type. Read more
Source§

impl Encode for BranchHints

Source§

fn encode(&self, sink: &mut Vec<u8>)

Encode the type into the given byte sink.
Source§

impl Section for BranchHints

Source§

fn id(&self) -> u8

Gets the section identifier for this section.
Source§

fn append_to(&self, dst: &mut Vec<u8>)

Appends this section to the specified destination list of bytes.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.