Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rustler/src/types/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ impl OwnedBinary {
})
}

/// Copies 'data''s data into a new `OwnedBinary`.
pub fn from_slice(data: impl AsRef<[u8]>) -> Self {
Comment thread
filmor marked this conversation as resolved.
let data = data.as_ref();
let mut bin = OwnedBinary::new(data.len()).expect("allocation failed");
bin.as_mut_slice().copy_from_slice(data);
bin
}

/// Attempts to reallocate `self` with the new size.
///
/// Memory outside the range of the original binary will not be initialized. If
Expand Down Expand Up @@ -262,6 +270,12 @@ impl FromIterator<u8> for OwnedBinary {
}
}

impl<T: AsRef<[u8]>> From<T> for OwnedBinary {
fn from(data: T) -> Self {
Comment thread
filmor marked this conversation as resolved.
Self::from_slice(data)
}
}

/// An immutable smart-pointer to an Erlang binary.
///
/// See [module-level doc](index.html) for more information.
Expand Down
8 changes: 8 additions & 0 deletions rustler_tests/lib/rustler_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ defmodule RustlerTest do
def decode_iolist(_), do: err()
def first_four_bytes_of_iolist(_), do: err()

def array_into_binary(), do: err()
Comment thread
filmor marked this conversation as resolved.
def vec_into_binary(), do: err()
def slice_into_binary(), do: err()
def str_into_binary(), do: err()
def string_into_binary(), do: err()

def binary_from_str(), do: err()

def atom_to_string(_), do: err()
def atom_equals_ok(_), do: err()
def binary_to_atom(_), do: err()
Expand Down
30 changes: 30 additions & 0 deletions rustler_tests/native/rustler_test/src/test_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,33 @@ pub fn first_four_bytes_of_iolist<'a>(term: Term<'a>) -> Binary<'a> {
let sub = bin.make_subbinary(0, 4).unwrap();
sub
}

#[rustler::nif]
pub fn array_into_binary() -> OwnedBinary {
[1, 2, 3].into()
}

#[rustler::nif]
pub fn vec_into_binary() -> OwnedBinary {
vec![1, 2, 3].into()
}

#[rustler::nif]
pub fn slice_into_binary() -> OwnedBinary {
[1, 2, 3][..].into()
}

#[rustler::nif]
pub fn str_into_binary() -> OwnedBinary {
"foobar".into()
}

#[rustler::nif]
pub fn string_into_binary() -> OwnedBinary {
String::from("foobar").into()
}

#[rustler::nif]
pub fn binary_from_str() -> OwnedBinary {
OwnedBinary::from("foobar")
}
12 changes: 12 additions & 0 deletions rustler_tests/test/binary_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ defmodule RustlerTest.BinaryTest do
assert RustlerTest.first_four_bytes_of_iolist(["hi", " ", "there"]) == "hi t"
assert RustlerTest.first_four_bytes_of_iolist([[?h, ?i], ~c" ", ["there"]]) == "hi t"
end

test "trait Into" do
assert RustlerTest.array_into_binary() == <<1, 2, 3>>
assert RustlerTest.vec_into_binary() == <<1, 2, 3>>
assert RustlerTest.slice_into_binary() == <<1, 2, 3>>
assert RustlerTest.str_into_binary() == "foobar"
assert RustlerTest.string_into_binary() == "foobar"
end

test "trait From" do
assert RustlerTest.binary_from_str() == "foobar"
end
end