Skip to content

Commit

Permalink
add encoder for &str
Browse files Browse the repository at this point in the history
  • Loading branch information
hansihe committed Jun 16, 2018
1 parent 49623a0 commit f4d35a8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rustler/src/types/string.rs
Expand Up @@ -19,6 +19,12 @@ impl<'a> Decoder<'a> for &'a str {

use std::io::Write;

impl<'a> Encoder for &'a str {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
(*self).encode(env)
}
}

impl Encoder for str {
fn encode<'b>(&self, env: Env<'b>) -> Term<'b> {
let str_len = self.len();
Expand Down
1 change: 1 addition & 0 deletions rustler_tests/lib/rustler_test.ex
Expand Up @@ -37,6 +37,7 @@ defmodule RustlerTest do
def unowned_to_owned(_), do: err()
def realloc_shrink(), do: err()
def realloc_grow(), do: err()
def encode_string(), do: err()

def atom_to_string(_), do: err()
def atom_equals_ok(_), do: err()
Expand Down
1 change: 1 addition & 0 deletions rustler_tests/src/lib.rs
Expand Up @@ -55,6 +55,7 @@ rustler_export_nifs!(
("unowned_to_owned", 1, test_binary::unowned_to_owned),
("realloc_shrink", 0, test_binary::realloc_shrink),
("realloc_grow", 0, test_binary::realloc_grow),
("encode_string", 0, test_binary::encode_string),

("threaded_fac", 1, test_thread::threaded_fac),
("threaded_sleep", 1, test_thread::threaded_sleep),
Expand Down
4 changes: 4 additions & 0 deletions rustler_tests/src/test_binary.rs
Expand Up @@ -48,6 +48,10 @@ pub fn realloc_grow<'a>(env: Env<'a>, _args: &[Term<'a>]) -> NifResult<Term<'a>>
Ok(binary.release(env).encode(env))
}

pub fn encode_string<'a>(env: Env<'a>, _args: &[Term<'a>]) -> NifResult<Term<'a>> {
Ok(("first".to_string(), "second").encode(env))
}

// OwnedBinary::new
// OwnedBinary::from_unowned
// OwnedBinary::realloc
Expand Down
4 changes: 4 additions & 0 deletions rustler_tests/test/binary_test.exs
Expand Up @@ -31,4 +31,8 @@ defmodule RustlerTest.BinaryTest do
assert RustlerTest.realloc_grow() == <<1, 2, 3, 4, 5>>
end

test "encode string" do
assert RustlerTest.encode_string() == {"first", "second"}
end

end

0 comments on commit f4d35a8

Please sign in to comment.