This is an automated email from the ASF dual-hosted git repository.
silver pushed a commit to branch cpp-binding
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/cpp-binding by this push:
new 520754638 add error handle
520754638 is described below
commit 5207546384fb00c6a43e83d2de0bc114bc6ba49d
Author: silver-ymz <[email protected]>
AuthorDate: Wed Aug 30 23:43:59 2023 +0800
add error handle
Signed-off-by: silver-ymz <[email protected]>
---
Cargo.lock | 1 +
bindings/cpp/Cargo.toml | 1 +
bindings/cpp/include/opendal.hpp | 1 -
bindings/cpp/src/lib.rs | 28 +++++++++++++++-------------
bindings/cpp/src/opendal.cpp | 1 -
5 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index eb4beb544..69576424d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3575,6 +3575,7 @@ dependencies = [
name = "opendal-cpp"
version = "0.1.0"
dependencies = [
+ "anyhow",
"cxx",
"cxx-build",
"opendal",
diff --git a/bindings/cpp/Cargo.toml b/bindings/cpp/Cargo.toml
index 1323eac83..55fcce62f 100644
--- a/bindings/cpp/Cargo.toml
+++ b/bindings/cpp/Cargo.toml
@@ -33,6 +33,7 @@ crate-type = ["staticlib"]
[dependencies]
opendal.workspace = true
cxx = "1.0"
+anyhow = "1.0"
[build-dependencies]
cxx-build = "1.0"
diff --git a/bindings/cpp/include/opendal.hpp b/bindings/cpp/include/opendal.hpp
index 4a5582140..2d42b8390 100644
--- a/bindings/cpp/include/opendal.hpp
+++ b/bindings/cpp/include/opendal.hpp
@@ -22,7 +22,6 @@
#include <optional>
#include <string>
-#include <string_view>
#include <unordered_map>
#include <vector>
diff --git a/bindings/cpp/src/lib.rs b/bindings/cpp/src/lib.rs
index d574fe439..ff9f68e12 100644
--- a/bindings/cpp/src/lib.rs
+++ b/bindings/cpp/src/lib.rs
@@ -15,8 +15,10 @@
// specific language governing permissions and limitations
// under the License.
+use anyhow::Result;
use opendal as od;
-use std::{collections::HashMap, str::FromStr};
+use std::collections::HashMap;
+use std::str::FromStr;
#[cxx::bridge(namespace = "opendal::ffi")]
mod ffi {
@@ -28,33 +30,33 @@ mod ffi {
extern "Rust" {
type Operator;
- fn new_operator(scheme: &str, configs: Vec<HashMapValue>) ->
Box<Operator>;
- fn read(&self, path: &str) -> Vec<u8>;
- fn write(&self, path: &str, bs: &[u8]);
+ fn new_operator(scheme: &str, configs: Vec<HashMapValue>) ->
Result<Box<Operator>>;
+ fn read(&self, path: &str) -> Result<Vec<u8>>;
+ fn write(&self, path: &str, bs: &[u8]) -> Result<()>;
}
}
struct Operator(od::BlockingOperator);
-fn new_operator(scheme: &str, configs: Vec<ffi::HashMapValue>) ->
Box<Operator> {
- let scheme = od::Scheme::from_str(scheme).unwrap();
+fn new_operator(scheme: &str, configs: Vec<ffi::HashMapValue>) ->
Result<Box<Operator>> {
+ let scheme = od::Scheme::from_str(scheme)?;
let map = configs
.into_iter()
.map(|value| (value.key, value.value))
.collect::<HashMap<_, _>>();
- Box::new(Operator(
- od::Operator::via_map(scheme, map).unwrap().blocking(),
- ))
+ let op = Box::new(Operator(od::Operator::via_map(scheme,
map)?.blocking()));
+
+ Ok(op)
}
impl Operator {
- fn read(&self, path: &str) -> Vec<u8> {
- self.0.read(path).unwrap()
+ fn read(&self, path: &str) -> Result<Vec<u8>> {
+ Ok(self.0.read(path)?)
}
- fn write(&self, path: &str, bs: &[u8]) {
- self.0.write(path, bs.to_owned()).unwrap()
+ fn write(&self, path: &str, bs: &[u8]) -> Result<()> {
+ Ok(self.0.write(path, bs.to_owned())?)
}
}
diff --git a/bindings/cpp/src/opendal.cpp b/bindings/cpp/src/opendal.cpp
index d45c12ea0..92c240c3e 100644
--- a/bindings/cpp/src/opendal.cpp
+++ b/bindings/cpp/src/opendal.cpp
@@ -18,7 +18,6 @@
*/
#include "opendal.hpp"
-#include "lib.rs.h"
using namespace opendal;