Struct bpf_script::Compiler

source ·
pub struct Compiler<'a> { /* private fields */ }

Implementations

Create a new compiler instance.

Arguments
  • types - The BTF type library to use when resolving types.
Example
use bpf_script::{Compiler, TypeDatabase};

let mut database = TypeDatabase::default();
let mut compiler = Compiler::create(&database);

Used to capture variables from the outer scope into the BPF program being compiled. This is mostly used to capture map identifers to pass to BPF helpers and for other integer values that need to be captured. In the future, this will be extended to capture arbitrary types making sharing between Rust and BPF more seamless.

Arguments

name - The name of the variable when referenced from the script. value - The value of the variable.

Example
use bpf_script::{Compiler, TypeDatabase};

let mut database = TypeDatabase::default();
let mut compiler = Compiler::create(&database);
compiler.capture("outer", 0xdeadbeef);
compiler.compile(r#"
    fn()
        return outer
"#).expect("Failed to compile.");

Compile a given script.

Arguments
  • script_text - The script to compile, as a string.
Example
use bpf_script::{Compiler, TypeDatabase};

let mut database = TypeDatabase::default();
database.add_integer(Some("u32"), 4, false);
let mut compiler = Compiler::create(&database);
compiler.compile(r#"
    fn(a: u32)
        return a
"#).expect("Failed to compile.");

Returns the internally held instructions after compile has been called.

Example
use bpf_script::{Compiler, TypeDatabase};

let mut database = TypeDatabase::default();
database.add_integer(Some("u32"), 4, false);
let mut compiler = Compiler::create(&database);
compiler.compile(r#"
    fn(a: u32)
        return a
"#).expect("Failed to compile.");
for ins in compiler.get_instructions() {
    println!("{}", ins);
}

Returns the bytecode of a program after compile has been called. These are the raw instructions that make up a BPF program that can be passed directly to the kernel.

Example
use bpf_script::{Compiler, TypeDatabase};

let mut database = TypeDatabase::default();
database.add_integer(Some("u32"), 4, false);
let mut compiler = Compiler::create(&database);
compiler.compile(r#"
    fn(a: u32)
        return a
"#).expect("Failed to compile.");
for ins in compiler.get_bytecode() {
    println!("{}", ins);
}

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.