Skip to content

Commit

Permalink
[Rust] Utility for WasmEdgeString types (WasmEdge#631)
Browse files Browse the repository at this point in the history
* [CI] Fix inventory ci on rust-sys

Signed-off-by: Antonio Yang <[email protected]>

* [Rust] Utility for WasmEdgeString types

- Implement PartialEq of string types
- Implement CString convert traits
- implement into_bytes() for WasmEdgeString

Signed-off-by: Antonio Yang <[email protected]>

Co-authored-by: hydai <[email protected]>
  • Loading branch information
yanganto and hydai authored Nov 10, 2021
1 parent 1bbbcba commit 3b60feb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ on:
branches:
- master
paths:
- 'bindings/rust/wasmedge-sys'
- 'bindings/rust/wasmedge-sys/**'
pull_request:
branches:
- master
paths:
- 'bindings/rust/wasmedge-sys'
- 'bindings/rust/wasmedge-sys/**'

jobs:
inventorying rust:
rust:
runs-on: ubuntu-latest
container:
image: wasmedge/wasmedge:ubuntu-build-clang
Expand Down
77 changes: 75 additions & 2 deletions bindings/rust/wasmedge-sys/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
use super::wasmedge;
use std::marker::PhantomData;

use std::{convert::TryInto, ffi::CString, marker::PhantomData};

#[derive(Clone, Debug)]
pub enum WasmEdgeString<'a> {
Owned(StringBuf),
Borrowed(StringRef<'a>),
}

impl PartialEq for WasmEdgeString<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(WasmEdgeString::Owned(b), WasmEdgeString::Borrowed(r))
| (WasmEdgeString::Borrowed(r), WasmEdgeString::Owned(b)) => unsafe {
wasmedge::WasmEdge_StringIsEqual(
wasmedge::WasmEdge_StringWrap(b.inner.Buf, b.inner.Length),
r.inner,
)
},
(WasmEdgeString::Owned(b), WasmEdgeString::Owned(other_b)) => b == other_b,
(WasmEdgeString::Borrowed(r), WasmEdgeString::Borrowed(other_r)) => r == other_r,
}
}
}

impl Drop for WasmEdgeString<'_> {
fn drop(&mut self) {
if let Self::Owned(buf) = self {
Expand All @@ -22,6 +39,13 @@ impl WasmEdgeString<'_> {
Self::Borrowed(b) => Self::Owned(b.to_owned()),
}
}

pub fn into_bytes(self) -> Vec<u8> {
match self {
Self::Owned(o) => o.into_bytes(),
Self::Borrowed(b) => b.into_bytes(),
}
}
}

impl From<StringBuf> for WasmEdgeString<'_> {
Expand All @@ -30,6 +54,13 @@ impl From<StringBuf> for WasmEdgeString<'_> {
}
}

impl From<CString> for WasmEdgeString<'_> {
fn from(s: CString) -> Self {
let buf: StringBuf = s.into();
buf.into()
}
}

impl<'a, T: Into<StringRef<'a>>> From<T> for WasmEdgeString<'a> {
fn from(str_ref: T) -> Self {
Self::Borrowed(str_ref.into())
Expand All @@ -51,18 +82,42 @@ impl Default for wasmedge::WasmEdge_String {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Copy)]
pub struct StringBuf {
pub(crate) inner: wasmedge::WasmEdge_String,
}

impl PartialEq for StringBuf {
fn eq(&self, other: &Self) -> bool {
unsafe {
wasmedge::WasmEdge_StringIsEqual(
wasmedge::WasmEdge_StringWrap(self.inner.Buf, self.inner.Length),
wasmedge::WasmEdge_StringWrap(other.inner.Buf, other.inner.Length),
)
}
}
}

impl<'a> StringBuf {
pub fn as_ref(&'a self) -> StringRef<'a> {
StringRef {
inner: unsafe { wasmedge::WasmEdge_StringWrap(self.inner.Buf, self.inner.Length) },
lifetime: PhantomData,
}
}

pub fn into_bytes(self) -> Vec<u8> {
let mut vec = Vec::with_capacity(
self.inner
.Length
.try_into()
.expect("buffer size should smaller than the platform usize"),
);
unsafe {
wasmedge::WasmEdge_StringCopy(self.inner, vec.as_mut_ptr(), self.inner.Length);
}
vec.into_iter().map(|i| i as u8).collect()
}
}

impl From<String> for StringBuf {
Expand All @@ -76,6 +131,14 @@ impl From<String> for StringBuf {
}
}

impl From<CString> for StringBuf {
fn from(s: CString) -> Self {
Self {
inner: unsafe { wasmedge::WasmEdge_StringCreateByCString(s.as_ptr()) },
}
}
}

impl Clone for StringBuf {
fn clone(&self) -> Self {
Self {
Expand All @@ -92,6 +155,12 @@ pub struct StringRef<'a> {
lifetime: PhantomData<&'a ()>,
}

impl PartialEq for StringRef<'_> {
fn eq(&self, other: &Self) -> bool {
unsafe { wasmedge::WasmEdge_StringIsEqual(self.inner, other.inner) }
}
}

impl StringRef<'_> {
pub fn to_owned(self) -> StringBuf {
StringBuf {
Expand All @@ -100,6 +169,10 @@ impl StringRef<'_> {
},
}
}

pub fn into_bytes(self) -> Vec<u8> {
self.to_owned().into_bytes()
}
}

impl<'a> From<&'a str> for StringRef<'a> {
Expand Down

0 comments on commit 3b60feb

Please sign in to comment.