marine_macro/
lib.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Defines the #[marine] macro that should be used with all export functions, extern blocks.
18//! At now, It supports the following types that could be used as parameters in export or foreign
19//! functions: i8, i16, i32, i64, u8, u16, u32, u64, f32, f64, bool, String, Vec<u8>. Also struct
20//! where all fields are public and have aforementioned types could be used as parameters. In this
21//! case #[marine] should be also applied to this structs.
22//!
23//! # Examples
24//!
25//! This example shows how a function could be exported:
26//! ```ignore
27//! #[marine]
28//! pub fn greeting(name: String) -> String {
29//!     format!("Hi {}", name)
30//! }
31//! ```
32//!
33//! This more complex example shows how a function could be imported from another Wasm module
34//! and how a struct could be passed:
35//!
36//! ```ignore
37//! use marine_rs_sdk::MountedBinaryResult;
38//!
39//! #[marine]
40//! pub fn read_ipfs_file(file_path: String) -> MountedBinaryResult {
41//!     let hash = calculate_hash(file_path);
42//!     ipfs(vec![hash])
43//! }
44//!
45//! #[marine]
46//! #[link(wasm_import_module = "ipfs_node")]
47//! extern "C" {
48//!     pub fn ipfs(file_hash: Vec<String>) -> MountedBinaryResult;
49//! }
50//!
51//! ```
52
53#![doc(html_root_url = "https://docs.rs/marine-macro/0.7.1")] // x-release-please-version
54#![deny(
55    dead_code,
56    nonstandard_style,
57    unused_imports,
58    unused_mut,
59    unused_unsafe,
60    unreachable_patterns
61)]
62#![warn(rust_2018_idioms)]
63#![recursion_limit = "1024"]
64
65use marine_macro_impl::marine as marine_impl;
66use proc_macro::TokenStream;
67
68#[proc_macro_attribute]
69pub fn marine(_attr: TokenStream, input: TokenStream) -> TokenStream {
70    // into converts proc_macro::TokenStream to proc_macro2::TokenStream
71    match marine_impl(input.into()) {
72        Ok(v) => v,
73        // converts syn:error to proc_macro2::TokenStream
74        Err(e) => e.to_compile_error(),
75    }
76    // converts proc_macro2::TokenStream to proc_macro::TokenStream
77    .into()
78}
79
80// deprecated macro for backwards compatibility
81#[deprecated(since = "0.6.2", note = "please use the #[marine] macro instead")]
82#[proc_macro_attribute]
83pub fn fce(_attr: TokenStream, input: TokenStream) -> TokenStream {
84    // into converts proc_macro::TokenStream to proc_macro2::TokenStream
85    match marine_impl(input.into()) {
86        Ok(v) => v,
87        // converts syn:error to proc_macro2::TokenStream
88        Err(e) => e.to_compile_error(),
89    }
90    // converts proc_macro2::TokenStream to proc_macro::TokenStream
91    .into()
92}