From f4d35a873d050961b3c1211aaf908423c143153d Mon Sep 17 00:00:00 2001 From: HansiHE Date: Sat, 16 Jun 2018 23:40:03 +0200 Subject: [PATCH] add encoder for &str --- rustler/src/types/string.rs | 6 ++++++ rustler_tests/lib/rustler_test.ex | 1 + rustler_tests/src/lib.rs | 1 + rustler_tests/src/test_binary.rs | 4 ++++ rustler_tests/test/binary_test.exs | 4 ++++ 5 files changed, 16 insertions(+) diff --git a/rustler/src/types/string.rs b/rustler/src/types/string.rs index ced76fad..c62b5395 100644 --- a/rustler/src/types/string.rs +++ b/rustler/src/types/string.rs @@ -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(); diff --git a/rustler_tests/lib/rustler_test.ex b/rustler_tests/lib/rustler_test.ex index d6f49d87..827a619f 100644 --- a/rustler_tests/lib/rustler_test.ex +++ b/rustler_tests/lib/rustler_test.ex @@ -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() diff --git a/rustler_tests/src/lib.rs b/rustler_tests/src/lib.rs index 0ee81db1..3db1858b 100644 --- a/rustler_tests/src/lib.rs +++ b/rustler_tests/src/lib.rs @@ -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), diff --git a/rustler_tests/src/test_binary.rs b/rustler_tests/src/test_binary.rs index 21495698..f3112fb9 100644 --- a/rustler_tests/src/test_binary.rs +++ b/rustler_tests/src/test_binary.rs @@ -48,6 +48,10 @@ pub fn realloc_grow<'a>(env: Env<'a>, _args: &[Term<'a>]) -> NifResult> Ok(binary.release(env).encode(env)) } +pub fn encode_string<'a>(env: Env<'a>, _args: &[Term<'a>]) -> NifResult> { + Ok(("first".to_string(), "second").encode(env)) +} + // OwnedBinary::new // OwnedBinary::from_unowned // OwnedBinary::realloc diff --git a/rustler_tests/test/binary_test.exs b/rustler_tests/test/binary_test.exs index 0154dd74..217e6b40 100644 --- a/rustler_tests/test/binary_test.exs +++ b/rustler_tests/test/binary_test.exs @@ -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