Skip to content

Recommended way for passing binary data #130

@cquintana92

Description

@cquintana92

Hi!
First of all, thanks for developing this crate, it works wonderfully :)

However, there is a scenario that I'd like to use this crate that I don't fully understand how to do it. I'd like to pass a binary data string to Rust code, and despite having a look at #31 I don't fully understand how to use this.

I have the following code

fn do_something(input: &[u8]) -> u64 {
  // Do stuff
}

/// Computes some things
///
/// @param $input string Your input.
///
/// @return int Your result
#[php_function]
pub fn computation_rs(input: &str) -> u64 {
    do_something(input.as_bytes())
}

// Required to register the extension with PHP.
#[php_module]
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
    module
}

And my PHP code looks like

$data = random_bytes(32);
$result = computation_rs($data);
echo $result;

If I replace $data with a valid string, this works, but I haven't managed to pass an array of bytes. I've tried setting the input type in rust to Vec<u8> but it didn't work either 🤔

Do you have any pointers?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions