Struct rspirv::binary::Parser

source ·
pub struct Parser<'c, 'd> { /* private fields */ }
Expand description

The SPIR-V binary parser.

Takes in a vector of bytes and a consumer, this parser will invoke the consume methods on the consumer for the module header and each instruction parsed.

Different from the Decoder, this parser is high-level; it has knowlege of the SPIR-V grammar. It will parse instructions according to SPIR-V grammar.

Examples

use spirv::{AddressingModel, MemoryModel};
use rspirv::binary::Parser;
use rspirv::dr::{Loader, Operand};

let bin = vec![
    // Magic number.           Version number: 1.0.
    0x03, 0x02, 0x23, 0x07,    0x00, 0x00, 0x01, 0x00,
    // Generator number: 0.    Bound: 0.
    0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00,
    // Reserved word: 0.
    0x00, 0x00, 0x00, 0x00,
    // OpMemoryModel.          Logical.
    0x0e, 0x00, 0x03, 0x00,    0x00, 0x00, 0x00, 0x00,
    // GLSL450.
    0x01, 0x00, 0x00, 0x00];
let mut loader = Loader::new();  // You can use your own consumer here.
{
    let p = Parser::new(&bin, &mut loader);
    p.parse().unwrap();
}
let module = loader.module();

assert_eq!((1, 0), module.header.unwrap().version());
let m = module.memory_model.as_ref().unwrap();
assert_eq!(Operand::AddressingModel(AddressingModel::Logical),
           m.operands[0]);
assert_eq!(Operand::MemoryModel(MemoryModel::GLSL450),
           m.operands[1]);

Implementations§

source§

impl<'c, 'd> Parser<'c, 'd>

source

pub fn new(binary: &'d [u8], consumer: &'c mut dyn Consumer) -> Self

Creates a new parser to parse the given binary and send the module header and instructions to the given consumer.

source

pub fn parse(self) -> Result<()>

Does the parsing.

Auto Trait Implementations§

§

impl<'c, 'd> !RefUnwindSafe for Parser<'c, 'd>

§

impl<'c, 'd> !Send for Parser<'c, 'd>

§

impl<'c, 'd> !Sync for Parser<'c, 'd>

§

impl<'c, 'd> Unpin for Parser<'c, 'd>

§

impl<'c, 'd> !UnwindSafe for Parser<'c, 'd>

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>,

§

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>,

§

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.