From d9e686d258696822510d660ef10ce679582cb074 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:00:44 +0800 Subject: [PATCH 01/18] feat(bindings/dotnet): basic structure Signed-off-by: tison --- bindings/dotnet/.gitignore | 394 ++++++++++++++++++ .../dotnet/DotOpenDAL/BlockingOperator.cs | 5 + bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj | 9 + bindings/dotnet/dotnet.sln | 16 + 4 files changed, 424 insertions(+) create mode 100644 bindings/dotnet/.gitignore create mode 100644 bindings/dotnet/DotOpenDAL/BlockingOperator.cs create mode 100644 bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj create mode 100644 bindings/dotnet/dotnet.sln diff --git a/bindings/dotnet/.gitignore b/bindings/dotnet/.gitignore new file mode 100644 index 000000000000..92bcb2ed7981 --- /dev/null +++ b/bindings/dotnet/.gitignore @@ -0,0 +1,394 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs new file mode 100644 index 000000000000..d863988d7a61 --- /dev/null +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -0,0 +1,5 @@ +namespace DotOpenDAL; + +public class BlockingOperator +{ +} diff --git a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj new file mode 100644 index 000000000000..6836c6808fb2 --- /dev/null +++ b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/bindings/dotnet/dotnet.sln b/bindings/dotnet/dotnet.sln new file mode 100644 index 000000000000..b1181ec2883b --- /dev/null +++ b/bindings/dotnet/dotnet.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotOpenDAL", "DotOpenDAL\DotOpenDAL.csproj", "{9DE3EBDB-D64A-4432-89A6-AB6BC1183935}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal From a12d744dff5b5405bbcd79aa27b90ac05870c968 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:06:52 +0800 Subject: [PATCH 02/18] chore: license header Signed-off-by: tison --- .../dotnet/DotOpenDAL/BlockingOperator.cs | 21 ++++++++++++++++++- licenserc.toml | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs index d863988d7a61..db4861c44b3f 100644 --- a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -1,4 +1,23 @@ -namespace DotOpenDAL; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +namespace DotOpenDAL; public class BlockingOperator { diff --git a/licenserc.toml b/licenserc.toml index e3a939cba43e..6f441de96915 100644 --- a/licenserc.toml +++ b/licenserc.toml @@ -37,5 +37,5 @@ excludes = [ inceptionYear = 2023 [mapping.SLASHSTAR_STYLE] -# use SLASH_STAR_STYLE licence header for C/C++ code -extensions = ["c", "cc", "cpp", "h", "hpp"] +# use SLASH_STAR_STYLE licence header for C/C++ and C# code +extensions = ["c", "cc", "cpp", "h", "hpp", "cs"] From c3cc29198c023c691d34cdf086a53046b93cf367 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:11:51 +0800 Subject: [PATCH 03/18] build: DotOpenDAL proj metadata Signed-off-by: tison --- bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj index 6836c6808fb2..6860f77b58af 100644 --- a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj +++ b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj @@ -1,6 +1,17 @@ + DotOpenDAL + The official .NET binding for Apache OpenDAL (incubating) + 0.1.0 + $(Version) + $(Version) + Apache OpenDAL (incubating) + Apache Software Foundation + $(Company) + Apache;OpenDAL + Apache-2.0 + net7.0 enable enable From 37ddb1a282eb3f8a2b8ad1c22099287b38781b1b Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:19:09 +0800 Subject: [PATCH 04/18] build: bindings lib Signed-off-by: tison --- Cargo.lock | 7 ++++ Cargo.toml | 1 + bindings/dotnet/.gitignore | 3 ++ bindings/dotnet/Cargo.toml | 35 +++++++++++++++++ bindings/dotnet/build.py | 78 ++++++++++++++++++++++++++++++++++++++ bindings/dotnet/src/lib.rs | 14 +++++++ licenserc.toml | 4 ++ 7 files changed, 142 insertions(+) create mode 100644 bindings/dotnet/Cargo.toml create mode 100755 bindings/dotnet/build.py create mode 100644 bindings/dotnet/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f58c0b7a7d39..108db71f811f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2774,6 +2774,13 @@ dependencies = [ "opendal", ] +[[package]] +name = "opendal-dotnet" +version = "0.37.0" +dependencies = [ + "opendal", +] + [[package]] name = "opendal-hs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3f0b7707357d..82b1569f3ba6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ members = [ "bindings/java", "bindings/haskell", "bindings/lua", + "bindings/dotnet", "bin/oli", "bin/oay", diff --git a/bindings/dotnet/.gitignore b/bindings/dotnet/.gitignore index 92bcb2ed7981..adb23ad6bc09 100644 --- a/bindings/dotnet/.gitignore +++ b/bindings/dotnet/.gitignore @@ -392,3 +392,6 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml + +# Rust targets +target diff --git a/bindings/dotnet/Cargo.toml b/bindings/dotnet/Cargo.toml new file mode 100644 index 000000000000..50092c6a9530 --- /dev/null +++ b/bindings/dotnet/Cargo.toml @@ -0,0 +1,35 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +[package] +name = "opendal-dotnet" +publish = false + +authors.workspace = true +edition.workspace = true +homepage.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true +version.workspace = true + +[lib] +crate-type = ["cdylib"] +doc = false + +[dependencies] +opendal.workspace = true diff --git a/bindings/dotnet/build.py b/bindings/dotnet/build.py new file mode 100755 index 000000000000..7996706ffe5a --- /dev/null +++ b/bindings/dotnet/build.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser +from pathlib import Path +import shutil +import subprocess + + +def classifier_to_target(classifier: str) -> str: + if classifier == 'osx-aarch_64': + return 'aarch64-apple-darwin' + if classifier == 'osx-x86_64': + return 'x86_64-apple-darwin' + if classifier == 'linux-x86_64': + return 'x86_64-unknown-linux-gnu' + if classifier == 'windows-x86_64': + return 'x86_64-pc-windows-msvc' + raise Exception(f'Unsupported classifier: {classifier}') + + +def get_cargo_artifact_name(classifier: str) -> str: + if classifier == 'osx-aarch_64': + return 'libopendal_dotnet.dylib' + if classifier == 'osx-x86_64': + return 'libopendal_dotnet.dylib' + if classifier == 'linux-x86_64': + return 'libopendal_dotnet.so' + if classifier == 'windows-x86_64': + return 'opendal_dotnet.dll' + raise Exception(f'Unsupported classifier: {classifier}') + + +if __name__ == '__main__': + basedir = Path(__file__).parent + + parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) + parser.add_argument('--classifier', type=str, required=True) + args = parser.parse_args() + + cmd = ['cargo', 'build', '--color=always', '--release'] + + target = classifier_to_target(args.classifier) + if target: + command = ['rustup', 'target', 'add', target] + print('$ ' + subprocess.list2cmdline(command)) + subprocess.run(command, cwd=basedir, check=True) + cmd += ['--target', target] + + output = basedir / 'target' / 'bindings' + Path(output).mkdir(exist_ok=True, parents=True) + cmd += ['--target-dir', output] + + print('$ ' + subprocess.list2cmdline(cmd)) + subprocess.run(cmd, cwd=basedir, check=True) + + artifact = get_cargo_artifact_name(args.classifier) + src = output / target / 'release' / artifact + dst = basedir / 'target' / 'native' / args.classifier / artifact + dst.parent.mkdir(exist_ok=True, parents=True) + shutil.copy2(src, dst) diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs new file mode 100644 index 000000000000..7d12d9af8195 --- /dev/null +++ b/bindings/dotnet/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/licenserc.toml b/licenserc.toml index 6f441de96915..60fbdfff0d47 100644 --- a/licenserc.toml +++ b/licenserc.toml @@ -31,6 +31,10 @@ excludes = [ # Website generated files "website/build", "website/.docusaurus", + + # Documents + "**/*.md", + "**/*.mdx", ] [properties] From 77237280a0b04d0a7e0ad1decb335e60ca2345b6 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:27:34 +0800 Subject: [PATCH 05/18] link Signed-off-by: tison --- bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj index 6860f77b58af..bcd7c0ef91ee 100644 --- a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj +++ b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj @@ -17,4 +17,13 @@ enable + + + Always + + + From 0046372bfee0bae7b77cbbc4fc552d7afe75d05e Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:30:45 +0800 Subject: [PATCH 06/18] demo binding Signed-off-by: tison --- bindings/dotnet/DotOpenDAL/BlockingOperator.cs | 4 ++++ bindings/dotnet/src/lib.rs | 16 +++------------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs index db4861c44b3f..87fb7fc0c0ea 100644 --- a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -17,8 +17,12 @@ * under the License. */ +using System.Runtime.InteropServices; + namespace DotOpenDAL; public class BlockingOperator { + [DllImport("opendal_dotnet", EntryPoint = "my_add", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern int my_add(int x, int y); } diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index 7d12d9af8195..d6b2fcdfa45c 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -1,14 +1,4 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +#[no_mangle] +pub extern "C" fn my_add(x: i32, y: i32) -> i32 { + x + y } From e1cc8dd9d07d1cb4a4d67239ffb6bc3301fba3b3 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 12:48:18 +0800 Subject: [PATCH 07/18] test strcuture Signed-off-by: tison --- .../DotOpenDAL.Tests/DotOpenDAL.Tests.csproj | 28 +++++++++++++++++++ bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs | 21 ++++++++++++++ bindings/dotnet/DotOpenDAL.Tests/Usings.cs | 1 + bindings/dotnet/dotnet.sln | 6 ++++ 4 files changed, 56 insertions(+) create mode 100644 bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj create mode 100644 bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs create mode 100644 bindings/dotnet/DotOpenDAL.Tests/Usings.cs diff --git a/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj b/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj new file mode 100644 index 000000000000..259579496d8f --- /dev/null +++ b/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj @@ -0,0 +1,28 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs new file mode 100644 index 000000000000..225af571c083 --- /dev/null +++ b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs @@ -0,0 +1,21 @@ +using Xunit.Abstractions; + +namespace DotOpenDAL.Tests; + +public class UnitTest1 +{ + + private readonly ITestOutputHelper output; + + public UnitTest1(ITestOutputHelper output) + { + this.output = output; + } + + [Fact] + public void Test1() + { + var x = BlockingOperator.my_add(1, 2); + output.WriteLine("{0}", x); + } +} diff --git a/bindings/dotnet/DotOpenDAL.Tests/Usings.cs b/bindings/dotnet/DotOpenDAL.Tests/Usings.cs new file mode 100644 index 000000000000..c802f4480b19 --- /dev/null +++ b/bindings/dotnet/DotOpenDAL.Tests/Usings.cs @@ -0,0 +1 @@ +global using Xunit; diff --git a/bindings/dotnet/dotnet.sln b/bindings/dotnet/dotnet.sln index b1181ec2883b..4325aa65a141 100644 --- a/bindings/dotnet/dotnet.sln +++ b/bindings/dotnet/dotnet.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotOpenDAL", "DotOpenDAL\DotOpenDAL.csproj", "{9DE3EBDB-D64A-4432-89A6-AB6BC1183935}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotOpenDAL.Tests", "DotOpenDAL.Tests\DotOpenDAL.Tests.csproj", "{060008AD-FEAD-49B8-97D0-3EFDC2505315}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Debug|Any CPU.Build.0 = Debug|Any CPU {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Release|Any CPU.ActiveCfg = Release|Any CPU {9DE3EBDB-D64A-4432-89A6-AB6BC1183935}.Release|Any CPU.Build.0 = Release|Any CPU + {060008AD-FEAD-49B8-97D0-3EFDC2505315}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {060008AD-FEAD-49B8-97D0-3EFDC2505315}.Debug|Any CPU.Build.0 = Debug|Any CPU + {060008AD-FEAD-49B8-97D0-3EFDC2505315}.Release|Any CPU.ActiveCfg = Release|Any CPU + {060008AD-FEAD-49B8-97D0-3EFDC2505315}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From 0fd18ef066d8427243f9575e7c8753607750298f Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 15:35:58 +0800 Subject: [PATCH 08/18] basic read write Signed-off-by: tison --- bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs | 3 +- .../dotnet/DotOpenDAL/BlockingOperator.cs | 39 ++++++++++++++- bindings/dotnet/src/lib.rs | 48 ++++++++++++++++++- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs index 225af571c083..c7acbe89a3ce 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs @@ -15,7 +15,6 @@ public UnitTest1(ITestOutputHelper output) [Fact] public void Test1() { - var x = BlockingOperator.my_add(1, 2); - output.WriteLine("{0}", x); + output.WriteLine("{0}", 42); } } diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs index 87fb7fc0c0ea..cc34ab306d84 100644 --- a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -23,6 +23,41 @@ namespace DotOpenDAL; public class BlockingOperator { - [DllImport("opendal_dotnet", EntryPoint = "my_add", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - public static extern int my_add(int x, int y); + private readonly IntPtr op; + + public BlockingOperator() + { + op = blocking_operator_construct("memory"); + } + + public void Write(string path) + { + blocking_operator_write(op, path); + } + + public string Read(string path) + { + return blocking_operator_read(op, path); + } + + [DllImport( + "opendal_dotnet", + EntryPoint = "blocking_operator_construct", + CallingConvention = CallingConvention.Cdecl, + CharSet = CharSet.Unicode)] + private static extern IntPtr blocking_operator_construct(string scheme); + + [DllImport( + "opendal_dotnet", + EntryPoint = "blocking_operator_read", + CallingConvention = CallingConvention.Cdecl, + CharSet = CharSet.Unicode)] + private static extern void blocking_operator_write(IntPtr op, string path); + + [DllImport( + "opendal_dotnet", + EntryPoint = "blocking_operator_read", + CallingConvention = CallingConvention.Cdecl, + CharSet = CharSet.Unicode)] + private static extern string blocking_operator_read(IntPtr op, string path); } diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index d6b2fcdfa45c..a0cb4b69066f 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -1,4 +1,48 @@ +use std::collections::HashMap; +use std::os::raw::c_char; +use std::str::FromStr; + #[no_mangle] -pub extern "C" fn my_add(x: i32, y: i32) -> i32 { - x + y +pub extern "C" fn blocking_operator_construct( + scheme: *const c_char, +) -> *const opendal::BlockingOperator { + if scheme.is_null() { + return std::ptr::null(); + } + + let scheme = match opendal::Scheme::from_str(unsafe { + std::ffi::CStr::from_ptr(scheme).to_str().unwrap() + }) { + Ok(scheme) => scheme, + Err(_) => return std::ptr::null(), + }; + + let map = HashMap::default(); + + let op = match opendal::Operator::via_map(scheme, map) { + Ok(op) => op.blocking(), + Err(_) => return std::ptr::null(), + }; + + Box::leak(Box::new(op)) +} + +#[no_mangle] +pub extern "C" fn blocking_operator_write( + op: *const opendal::BlockingOperator, + path: *const c_char, +) { + let op = unsafe { &*(op) }; + let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; + op.write(path, "12345").unwrap(); +} + +#[no_mangle] +pub extern "C" fn blocking_operator_read( + op: *const opendal::BlockingOperator, + path: *const c_char, +) { + let op = unsafe { &*(op) }; + let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; + let res = op.read(path).unwrap(); } From 254187677bfb860fca0c884973f192278a4252a7 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 16:07:36 +0800 Subject: [PATCH 09/18] almost mvp Signed-off-by: tison --- bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs | 6 +++++- bindings/dotnet/DotOpenDAL/BlockingOperator.cs | 16 ++++++++-------- bindings/dotnet/src/lib.rs | 12 +++++++++--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs index c7acbe89a3ce..e2c94f875e2e 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs @@ -15,6 +15,10 @@ public UnitTest1(ITestOutputHelper output) [Fact] public void Test1() { - output.WriteLine("{0}", 42); + var op = new BlockingOperator(); + output.WriteLine("{0}", op.Op.ToString()); + op.Write("test"); + var result = op.Read("test"); + output.WriteLine("{0}", result); } } diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs index cc34ab306d84..56ee767bc587 100644 --- a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -23,41 +23,41 @@ namespace DotOpenDAL; public class BlockingOperator { - private readonly IntPtr op; + public IntPtr Op { get; } public BlockingOperator() { - op = blocking_operator_construct("memory"); + Op = blocking_operator_construct("memory"); } public void Write(string path) { - blocking_operator_write(op, path); + blocking_operator_write(Op, path); } public string Read(string path) { - return blocking_operator_read(op, path); + return blocking_operator_read(Op, path); } [DllImport( "opendal_dotnet", EntryPoint = "blocking_operator_construct", CallingConvention = CallingConvention.Cdecl, - CharSet = CharSet.Unicode)] + CharSet = CharSet.Auto)] private static extern IntPtr blocking_operator_construct(string scheme); [DllImport( "opendal_dotnet", - EntryPoint = "blocking_operator_read", + EntryPoint = "blocking_operator_write", CallingConvention = CallingConvention.Cdecl, - CharSet = CharSet.Unicode)] + CharSet = CharSet.Auto)] private static extern void blocking_operator_write(IntPtr op, string path); [DllImport( "opendal_dotnet", EntryPoint = "blocking_operator_read", CallingConvention = CallingConvention.Cdecl, - CharSet = CharSet.Unicode)] + CharSet = CharSet.Auto)] private static extern string blocking_operator_read(IntPtr op, string path); } diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index a0cb4b69066f..bafc800913bf 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -17,11 +17,14 @@ pub extern "C" fn blocking_operator_construct( Err(_) => return std::ptr::null(), }; - let map = HashMap::default(); - + let mut map = HashMap::default(); + map.insert("root".to_string(), "/tmp".to_string()); let op = match opendal::Operator::via_map(scheme, map) { Ok(op) => op.blocking(), - Err(_) => return std::ptr::null(), + Err(err) => { + println!("err={err:?}"); + return std::ptr::null(); + } }; Box::leak(Box::new(op)) @@ -34,7 +37,10 @@ pub extern "C" fn blocking_operator_write( ) { let op = unsafe { &*(op) }; let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; + println!("path={path}"); op.write(path, "12345").unwrap(); + let res = op.read(path).unwrap(); + println!("res={res:?}"); } #[no_mangle] From 8ae4994544092961a96ac12c22060616ca3f4559 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 16:17:44 +0800 Subject: [PATCH 10/18] finish feat Signed-off-by: tison --- bindings/dotnet/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index bafc800913bf..0c08d85b5c5d 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -47,8 +47,12 @@ pub extern "C" fn blocking_operator_write( pub extern "C" fn blocking_operator_read( op: *const opendal::BlockingOperator, path: *const c_char, -) { +) -> *const c_char { let op = unsafe { &*(op) }; let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; - let res = op.read(path).unwrap(); + let mut res = op.read(path).unwrap(); + res.push(0); + std::ffi::CString::from_vec_with_nul(res) + .unwrap() + .into_raw() } From bca59bbf5db760cfcf2348fe40b12a548ffb3b2d Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 16:40:42 +0800 Subject: [PATCH 11/18] license header Signed-off-by: tison --- bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs | 19 +++++++++++++++++++ bindings/dotnet/DotOpenDAL.Tests/Usings.cs | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs index e2c94f875e2e..d312f9a8a71c 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + using Xunit.Abstractions; namespace DotOpenDAL.Tests; diff --git a/bindings/dotnet/DotOpenDAL.Tests/Usings.cs b/bindings/dotnet/DotOpenDAL.Tests/Usings.cs index c802f4480b19..8e6a8024ab43 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/Usings.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/Usings.cs @@ -1 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + global using Xunit; From 8bd07ca6381e705826fcdddb0082ce3db5934b7a Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 16:54:13 +0800 Subject: [PATCH 12/18] rename Signed-off-by: tison --- .../{UnitTest1.cs => BlockingOperatorTest.cs} | 8 ++++---- bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj | 6 +++--- bindings/dotnet/DotOpenDAL/BlockingOperator.cs | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) rename bindings/dotnet/DotOpenDAL.Tests/{UnitTest1.cs => BlockingOperatorTest.cs} (86%) diff --git a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs b/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs similarity index 86% rename from bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs rename to bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs index d312f9a8a71c..0d8a44537536 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/UnitTest1.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs @@ -21,23 +21,23 @@ namespace DotOpenDAL.Tests; -public class UnitTest1 +public class BlockingOperatorTest { private readonly ITestOutputHelper output; - public UnitTest1(ITestOutputHelper output) + public BlockingOperatorTest(ITestOutputHelper output) { this.output = output; } [Fact] - public void Test1() + public void TestReadWrite() { var op = new BlockingOperator(); output.WriteLine("{0}", op.Op.ToString()); op.Write("test"); - var result = op.Read("test"); + var result = op.Read("test", "12345"); output.WriteLine("{0}", result); } } diff --git a/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj b/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj index 259579496d8f..7209d89b2551 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj +++ b/bindings/dotnet/DotOpenDAL.Tests/DotOpenDAL.Tests.csproj @@ -10,11 +10,11 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs index 56ee767bc587..2958f1f354a6 100644 --- a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -35,9 +35,9 @@ public void Write(string path) blocking_operator_write(Op, path); } - public string Read(string path) + public string Read(string path, string content) { - return blocking_operator_read(Op, path); + return blocking_operator_read(Op, path, content); } [DllImport( @@ -59,5 +59,5 @@ public string Read(string path) EntryPoint = "blocking_operator_read", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)] - private static extern string blocking_operator_read(IntPtr op, string path); + private static extern string blocking_operator_read(IntPtr op, string path, string content); } From c1d304a20c3da07c3ca312fb20100f064f1d34c1 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 17:02:48 +0800 Subject: [PATCH 13/18] more clean up Signed-off-by: tison --- .../DotOpenDAL.Tests/BlockingOperatorTest.cs | 19 +++++-------------- .../dotnet/DotOpenDAL/BlockingOperator.cs | 13 +++++++------ bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj | 5 +---- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs b/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs index 0d8a44537536..e0b303faf8e3 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs @@ -17,27 +17,18 @@ * under the License. */ -using Xunit.Abstractions; - namespace DotOpenDAL.Tests; public class BlockingOperatorTest { - - private readonly ITestOutputHelper output; - - public BlockingOperatorTest(ITestOutputHelper output) - { - this.output = output; - } - [Fact] public void TestReadWrite() { var op = new BlockingOperator(); - output.WriteLine("{0}", op.Op.ToString()); - op.Write("test"); - var result = op.Read("test", "12345"); - output.WriteLine("{0}", result); + var content = "12345"; + Assert.NotEqual(op.Op, IntPtr.Zero); + op.Write("test", content); + var result = op.Read("test"); + Assert.Equal(content, result); } } diff --git a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs index 2958f1f354a6..9404e581db1f 100644 --- a/bindings/dotnet/DotOpenDAL/BlockingOperator.cs +++ b/bindings/dotnet/DotOpenDAL/BlockingOperator.cs @@ -17,6 +17,7 @@ * under the License. */ +using System; using System.Runtime.InteropServices; namespace DotOpenDAL; @@ -30,14 +31,14 @@ public BlockingOperator() Op = blocking_operator_construct("memory"); } - public void Write(string path) + public void Write(string path, string content) { - blocking_operator_write(Op, path); + blocking_operator_write(Op, path, content); } - public string Read(string path, string content) + public string Read(string path) { - return blocking_operator_read(Op, path, content); + return blocking_operator_read(Op, path); } [DllImport( @@ -52,12 +53,12 @@ public string Read(string path, string content) EntryPoint = "blocking_operator_write", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)] - private static extern void blocking_operator_write(IntPtr op, string path); + private static extern void blocking_operator_write(IntPtr op, string path, string content); [DllImport( "opendal_dotnet", EntryPoint = "blocking_operator_read", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)] - private static extern string blocking_operator_read(IntPtr op, string path, string content); + private static extern string blocking_operator_read(IntPtr op, string path); } diff --git a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj index bcd7c0ef91ee..2d4e1e8026aa 100644 --- a/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj +++ b/bindings/dotnet/DotOpenDAL/DotOpenDAL.csproj @@ -18,10 +18,7 @@ - + Always From 54abcd7a80d7ac1408e42d7ae2571f29e77d74b4 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 17:10:40 +0800 Subject: [PATCH 14/18] add content param Signed-off-by: tison --- bindings/dotnet/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index 0c08d85b5c5d..c079e5addbdc 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -34,10 +34,13 @@ pub extern "C" fn blocking_operator_construct( pub extern "C" fn blocking_operator_write( op: *const opendal::BlockingOperator, path: *const c_char, + _content: *const c_char, ) { let op = unsafe { &*(op) }; let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; - println!("path={path}"); + println!("path={path:?}"); + // let content = unsafe { std::ffi::CStr::from_ptr(content).to_str().unwrap() }; + // println!("content={content:?}"); op.write(path, "12345").unwrap(); let res = op.read(path).unwrap(); println!("res={res:?}"); @@ -50,8 +53,10 @@ pub extern "C" fn blocking_operator_read( ) -> *const c_char { let op = unsafe { &*(op) }; let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; + println!("path={path:?}"); let mut res = op.read(path).unwrap(); res.push(0); + println!("res={res:?}"); std::ffi::CString::from_vec_with_nul(res) .unwrap() .into_raw() From 43d7eb1012ee76a3c6dc09f8a664ccb3212b998f Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 17:16:13 +0800 Subject: [PATCH 15/18] license header Signed-off-by: tison --- bindings/dotnet/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index c079e5addbdc..2f56a5223a8c 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -1,3 +1,20 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + use std::collections::HashMap; use std::os::raw::c_char; use std::str::FromStr; From 6bc8f3074fc7fdf60bde5f26adfd475fa6334cb8 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 17:22:24 +0800 Subject: [PATCH 16/18] copy Signed-off-by: tison --- .../dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs | 2 +- bindings/dotnet/src/lib.rs | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs b/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs index e0b303faf8e3..aa8f2f2d6bab 100644 --- a/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs +++ b/bindings/dotnet/DotOpenDAL.Tests/BlockingOperatorTest.cs @@ -25,7 +25,7 @@ public class BlockingOperatorTest public void TestReadWrite() { var op = new BlockingOperator(); - var content = "12345"; + var content = "123456"; Assert.NotEqual(op.Op, IntPtr.Zero); op.Write("test", content); var result = op.Read("test"); diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index 2f56a5223a8c..effd127320b3 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -51,16 +51,12 @@ pub extern "C" fn blocking_operator_construct( pub extern "C" fn blocking_operator_write( op: *const opendal::BlockingOperator, path: *const c_char, - _content: *const c_char, + content: *const c_char, ) { let op = unsafe { &*(op) }; let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; - println!("path={path:?}"); - // let content = unsafe { std::ffi::CStr::from_ptr(content).to_str().unwrap() }; - // println!("content={content:?}"); - op.write(path, "12345").unwrap(); - let res = op.read(path).unwrap(); - println!("res={res:?}"); + let content = unsafe { std::ffi::CStr::from_ptr(content).to_str().unwrap() }; + op.write(path, content.to_owned()).unwrap() } #[no_mangle] @@ -70,10 +66,8 @@ pub extern "C" fn blocking_operator_read( ) -> *const c_char { let op = unsafe { &*(op) }; let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; - println!("path={path:?}"); let mut res = op.read(path).unwrap(); res.push(0); - println!("res={res:?}"); std::ffi::CString::from_vec_with_nul(res) .unwrap() .into_raw() From dd3e744ec53593477036a99be9f7e44c05e2f4db Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 17:36:02 +0800 Subject: [PATCH 17/18] clippy Signed-off-by: tison --- bindings/dotnet/src/lib.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/bindings/dotnet/src/lib.rs b/bindings/dotnet/src/lib.rs index effd127320b3..ba2415b9dd4b 100644 --- a/bindings/dotnet/src/lib.rs +++ b/bindings/dotnet/src/lib.rs @@ -19,17 +19,19 @@ use std::collections::HashMap; use std::os::raw::c_char; use std::str::FromStr; +/// # Safety +/// +/// Not yet. #[no_mangle] -pub extern "C" fn blocking_operator_construct( +pub unsafe extern "C" fn blocking_operator_construct( scheme: *const c_char, ) -> *const opendal::BlockingOperator { if scheme.is_null() { return std::ptr::null(); } - let scheme = match opendal::Scheme::from_str(unsafe { - std::ffi::CStr::from_ptr(scheme).to_str().unwrap() - }) { + let scheme = match opendal::Scheme::from_str(std::ffi::CStr::from_ptr(scheme).to_str().unwrap()) + { Ok(scheme) => scheme, Err(_) => return std::ptr::null(), }; @@ -47,25 +49,31 @@ pub extern "C" fn blocking_operator_construct( Box::leak(Box::new(op)) } +/// # Safety +/// +/// Not yet. #[no_mangle] -pub extern "C" fn blocking_operator_write( +pub unsafe extern "C" fn blocking_operator_write( op: *const opendal::BlockingOperator, path: *const c_char, content: *const c_char, ) { - let op = unsafe { &*(op) }; - let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; - let content = unsafe { std::ffi::CStr::from_ptr(content).to_str().unwrap() }; + let op = &*(op); + let path = std::ffi::CStr::from_ptr(path).to_str().unwrap(); + let content = std::ffi::CStr::from_ptr(content).to_str().unwrap(); op.write(path, content.to_owned()).unwrap() } +/// # Safety +/// +/// Not yet. #[no_mangle] -pub extern "C" fn blocking_operator_read( +pub unsafe extern "C" fn blocking_operator_read( op: *const opendal::BlockingOperator, path: *const c_char, ) -> *const c_char { - let op = unsafe { &*(op) }; - let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() }; + let op = &*(op); + let path = std::ffi::CStr::from_ptr(path).to_str().unwrap(); let mut res = op.read(path).unwrap(); res.push(0); std::ffi::CString::from_vec_with_nul(res) From a1c609b6f2d4a78cad865f4c85f143c199e1f457 Mon Sep 17 00:00:00 2001 From: tison Date: Sun, 18 Jun 2023 19:03:51 +0800 Subject: [PATCH 18/18] Update bindings/dotnet/Cargo.toml --- bindings/dotnet/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/dotnet/Cargo.toml b/bindings/dotnet/Cargo.toml index 50092c6a9530..c76fd5f4c64b 100644 --- a/bindings/dotnet/Cargo.toml +++ b/bindings/dotnet/Cargo.toml @@ -25,7 +25,7 @@ homepage.workspace = true license.workspace = true repository.workspace = true rust-version.workspace = true -version.workspace = true +version = "0.1.0" [lib] crate-type = ["cdylib"]