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.