From 5fecd9edf677267a088fb0718b1386526d55ebc3 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 24 Jan 2022 20:14:13 +0530 Subject: [PATCH] Support raw markers in struct fields (#47) --- deno_bindgen_macro/src/derive_struct.rs | 5 + example/bindings/bindings.ts | 192 +++++++++++++----------- example/bindings_test.ts | 13 ++ example/src/lib.rs | 15 ++ 4 files changed, 136 insertions(+), 89 deletions(-) diff --git a/deno_bindgen_macro/src/derive_struct.rs b/deno_bindgen_macro/src/derive_struct.rs index bfae6ed..ec2878c 100644 --- a/deno_bindgen_macro/src/derive_struct.rs +++ b/deno_bindgen_macro/src/derive_struct.rs @@ -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; @@ -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 { @@ -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 { diff --git a/example/bindings/bindings.ts b/example/bindings/bindings.ts index 403e5df..76c6d17 100644 --- a/example/bindings/bindings.ts +++ b/example/bindings/bindings.ts @@ -1,26 +1,26 @@ // Auto-generated with deno_bindgen -import { CachePolicy, prepare } from "https://deno.land/x/plug@0.4.1/plug.ts"; +import { CachePolicy, prepare } from "https://deno.land/x/plug@0.4.1/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 }, @@ -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", @@ -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 +} +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 @@ -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; -}; +} 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; - return result; + let result = _lib.symbols.sleep(a0) as Promise + 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; - result = result.then(read_pointer); - return result; + ) as Promise + 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; - result = result.then(read_pointer); - return result; + let result = _lib.symbols.test_manual_ptr_async() as Promise + 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; - result = result.then(read_pointer); - return result.then((r) => JSON.parse(decode(r))) as Promise; + let result = _lib.symbols.test_output_async() as Promise + result = result.then(read_pointer) + return result.then(r => JSON.parse(decode(r))) as Promise +} +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 } diff --git a/example/bindings_test.ts b/example/bindings_test.ts index 46f6d90..a39e32c 100644 --- a/example/bindings_test.ts +++ b/example/bindings_test.ts @@ -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"; @@ -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); + }, +}); + diff --git a/example/src/lib.rs b/example/src/lib.rs index e728edd..0a97358 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -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, + } +} +