Skip to content

Commit

Permalink
Support raw markers in struct fields (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Jan 24, 2022
1 parent 9e6646a commit 5fecd9e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 89 deletions.
5 changes: 5 additions & 0 deletions deno_bindgen_macro/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::docs::get_docs;
use crate::meta::Glue;

use std::collections::HashMap;
use syn::ext::IdentExt;
use syn::Data;
use syn::DataStruct;
use syn::DeriveInput;
Expand Down Expand Up @@ -47,6 +48,8 @@ pub fn process_struct(
.ident
.as_ref()
.expect("Field without ident")
// Strips the raw marker `r#`, if present.
.unraw()
.to_string();

match field.ty {
Expand Down Expand Up @@ -111,6 +114,8 @@ pub fn process_struct(
.ident
.as_ref()
.expect("Field without ident")
// Strips the raw marker `r#`, if present.
.unraw()
.to_string();

for attr in &serde_attrs {
Expand Down
192 changes: 103 additions & 89 deletions example/bindings/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
// Auto-generated with deno_bindgen
import { CachePolicy, prepare } from "https://deno.land/x/[email protected]/plug.ts";
import { CachePolicy, prepare } from "https://deno.land/x/[email protected]/plug.ts"
function encode(v: string | Uint8Array): Uint8Array {
if (typeof v !== "string") return v;
return new TextEncoder().encode(v);
if (typeof v !== "string") return v
return new TextEncoder().encode(v)
}
function decode(v: Uint8Array): string {
return new TextDecoder().decode(v);
return new TextDecoder().decode(v)
}
function read_pointer(v: any): Uint8Array {
const ptr = new Deno.UnsafePointerView(v as Deno.UnsafePointer);
const lengthBe = new Uint8Array(4);
const view = new DataView(lengthBe.buffer);
ptr.copyInto(lengthBe, 0);
const buf = new Uint8Array(view.getUint32(0));
ptr.copyInto(buf, 4);
return buf;
const ptr = new Deno.UnsafePointerView(v as Deno.UnsafePointer)
const lengthBe = new Uint8Array(4)
const view = new DataView(lengthBe.buffer)
ptr.copyInto(lengthBe, 0)
const buf = new Uint8Array(view.getUint32(0))
ptr.copyInto(buf, 4)
return buf
}
const opts = {
name: "deno_bindgen_test",
url: (new URL("../target/debug", import.meta.url)).toString(),
policy: CachePolicy.NONE,
};
}
const _lib = await prepare(opts, {
add: { parameters: ["i32", "i32"], result: "i32", nonblocking: false },
add2: { parameters: ["pointer", "usize"], result: "i32", nonblocking: false },
Expand Down Expand Up @@ -68,6 +68,11 @@ const _lib = await prepare(opts, {
},
test_output: { parameters: [], result: "pointer", nonblocking: false },
test_output_async: { parameters: [], result: "pointer", nonblocking: true },
test_reserved_field: {
parameters: [],
result: "pointer",
nonblocking: false,
},
test_serde: {
parameters: ["pointer", "usize"],
result: "u8",
Expand All @@ -83,7 +88,28 @@ const _lib = await prepare(opts, {
result: "i32",
nonblocking: false,
},
});
})
export type OptionStruct = {
maybe: string | undefined | null
}
export type MyStruct = {
arr: Array<string>
}
export type PlainEnum =
| {
a: {
_a: string
}
}
| "b"
| "c"
export type TestReservedField = {
type: number
ref: number
}
export type TestLifetimes = {
text: string
}
/**
* Doc comment for `Input` struct.
* ...testing multiline
Expand All @@ -94,133 +120,121 @@ export type Input = {
* transformed to JS doc
* comments.
*/
a: number;
b: number;
};
a: number
b: number
}
export type TestLifetimeEnums = {
Text: {
_text: string;
};
};
export type PlainEnum =
| {
a: {
_a: string;
};
_text: string
}
| "b"
| "c";
export type TestLifetimes = {
text: string;
};
export type MyStruct = {
arr: Array<string>;
};
}
export type TestLifetimeWrap = {
_a: TestLifetimeEnums;
};
_a: TestLifetimeEnums
}
export type TagAndContent =
| { key: "A"; value: { b: number } }
| { key: "C"; value: { d: number } };
export type OptionStruct = {
maybe: string | undefined | null;
};
| { key: "C"; value: { d: number } }
export function add(a0: number, a1: number) {
let result = _lib.symbols.add(a0, a1) as number;
return result;
let result = _lib.symbols.add(a0, a1) as number
return result
}
export function add2(a0: Input) {
const a0_buf = encode(JSON.stringify(a0));
let result = _lib.symbols.add2(a0_buf, a0_buf.byteLength) as number;
return result;
const a0_buf = encode(JSON.stringify(a0))
let result = _lib.symbols.add2(a0_buf, a0_buf.byteLength) as number
return result
}
export function sleep(a0: number) {
let result = _lib.symbols.sleep(a0) as Promise<null>;
return result;
let result = _lib.symbols.sleep(a0) as Promise<null>
return result
}
export function test_buf(a0: Uint8Array) {
const a0_buf = encode(a0);
let result = _lib.symbols.test_buf(a0_buf, a0_buf.byteLength) as number;
return result;
const a0_buf = encode(a0)
let result = _lib.symbols.test_buf(a0_buf, a0_buf.byteLength) as number
return result
}
export function test_buffer_return(a0: Uint8Array) {
const a0_buf = encode(a0);
const a0_buf = encode(a0)
let result = _lib.symbols.test_buffer_return(
a0_buf,
a0_buf.byteLength,
) as Uint8Array;
result = read_pointer(result);
return result;
) as Uint8Array
result = read_pointer(result)
return result
}
export function test_buffer_return_async(a0: Uint8Array) {
const a0_buf = encode(a0);
const a0_buf = encode(a0)
let result = _lib.symbols.test_buffer_return_async(
a0_buf,
a0_buf.byteLength,
) as Promise<Uint8Array>;
result = result.then(read_pointer);
return result;
) as Promise<Uint8Array>
result = result.then(read_pointer)
return result
}
export function test_lifetime(a0: TestLifetimes) {
const a0_buf = encode(JSON.stringify(a0));
let result = _lib.symbols.test_lifetime(a0_buf, a0_buf.byteLength) as number;
return result;
const a0_buf = encode(JSON.stringify(a0))
let result = _lib.symbols.test_lifetime(a0_buf, a0_buf.byteLength) as number
return result
}
export function test_manual_ptr() {
let result = _lib.symbols.test_manual_ptr() as Uint8Array;
result = read_pointer(result);
return result;
let result = _lib.symbols.test_manual_ptr() as Uint8Array
result = read_pointer(result)
return result
}
export function test_manual_ptr_async() {
let result = _lib.symbols.test_manual_ptr_async() as Promise<Uint8Array>;
result = result.then(read_pointer);
return result;
let result = _lib.symbols.test_manual_ptr_async() as Promise<Uint8Array>
result = result.then(read_pointer)
return result
}
export function test_mixed(a0: number, a1: Input) {
const a1_buf = encode(JSON.stringify(a1));
let result = _lib.symbols.test_mixed(a0, a1_buf, a1_buf.byteLength) as number;
return result;
const a1_buf = encode(JSON.stringify(a1))
let result = _lib.symbols.test_mixed(a0, a1_buf, a1_buf.byteLength) as number
return result
}
export function test_mixed_order(a0: number, a1: Input, a2: number) {
const a1_buf = encode(JSON.stringify(a1));
const a1_buf = encode(JSON.stringify(a1))
let result = _lib.symbols.test_mixed_order(
a0,
a1_buf,
a1_buf.byteLength,
a2,
) as number;
return result;
) as number
return result
}
export function test_mut_buf(a0: Uint8Array) {
const a0_buf = encode(a0);
let result = _lib.symbols.test_mut_buf(a0_buf, a0_buf.byteLength) as null;
return result;
const a0_buf = encode(a0)
let result = _lib.symbols.test_mut_buf(a0_buf, a0_buf.byteLength) as null
return result
}
export function test_output() {
let result = _lib.symbols.test_output() as Uint8Array;
result = read_pointer(result);
return JSON.parse(decode(result)) as Input;
let result = _lib.symbols.test_output() as Uint8Array
result = read_pointer(result)
return JSON.parse(decode(result)) as Input
}
export function test_output_async() {
let result = _lib.symbols.test_output_async() as Promise<Uint8Array>;
result = result.then(read_pointer);
return result.then((r) => JSON.parse(decode(r))) as Promise<Input>;
let result = _lib.symbols.test_output_async() as Promise<Uint8Array>
result = result.then(read_pointer)
return result.then(r => JSON.parse(decode(r))) as Promise<Input>
}
export function test_reserved_field() {
let result = _lib.symbols.test_reserved_field() as Uint8Array
result = read_pointer(result)
return JSON.parse(decode(result)) as TestReservedField
}
export function test_serde(a0: MyStruct) {
const a0_buf = encode(JSON.stringify(a0));
let result = _lib.symbols.test_serde(a0_buf, a0_buf.byteLength) as number;
return result;
const a0_buf = encode(JSON.stringify(a0))
let result = _lib.symbols.test_serde(a0_buf, a0_buf.byteLength) as number
return result
}
export function test_str(a0: string) {
const a0_buf = encode(a0);
let result = _lib.symbols.test_str(a0_buf, a0_buf.byteLength) as null;
return result;
const a0_buf = encode(a0)
let result = _lib.symbols.test_str(a0_buf, a0_buf.byteLength) as null
return result
}
export function test_tag_and_content(a0: TagAndContent) {
const a0_buf = encode(JSON.stringify(a0));
const a0_buf = encode(JSON.stringify(a0))
let result = _lib.symbols.test_tag_and_content(
a0_buf,
a0_buf.byteLength,
) as number;
return result;
) as number
return result
}
13 changes: 13 additions & 0 deletions example/bindings_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
test_serde,
test_str,
test_tag_and_content,
test_reserved_field,
TestReservedField,
} from "./bindings/bindings.ts";
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";

Expand Down Expand Up @@ -177,3 +179,14 @@ Deno.test({
assertEquals(obj.b, 4);
},
});

Deno.test({
name: "test_reserved_field#test",
fn: () => {
const obj = test_reserved_field();

assertEquals(obj.type, 1);
assertEquals(obj.ref, 2);
},
});

15 changes: 15 additions & 0 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,18 @@ fn test_output_async() -> Input {
b: 4
}
}

#[deno_bindgen]
struct TestReservedField {
r#type: u8,
r#ref: u8,
}

#[deno_bindgen]
fn test_reserved_field() -> TestReservedField {
TestReservedField {
r#type: 1,
r#ref: 2,
}
}

0 comments on commit 5fecd9e

Please sign in to comment.