Sober7135 commented on code in PR #822: URL: https://github.com/apache/incubator-graphar/pull/822#discussion_r2678478428
########## rust/src/types.rs: ########## @@ -0,0 +1,265 @@ +// 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 crate::ffi; +use cxx::SharedPtr; +use std::fmt::{Debug, Display}; + +// C++ enums +pub use crate::ffi::graphar::Type; + +#[derive(Clone)] +pub struct DataType(SharedPtr<ffi::graphar::DataType>); + +impl PartialEq for DataType { + fn eq(&self, other: &Self) -> bool { + if self.0.is_null() && other.0.is_null() { + return true; + } + if self.0.is_null() || other.0.is_null() { + return false; + } + + self.0.Equals(other.0.as_ref().expect("rhs is nullptr")) + } +} + +impl Eq for DataType {} + +impl Display for DataType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.0.is_null() { + write!(f, "null") + } else { + write!( + f, + "{}", + ffi::graphar::to_type_name(self.0.as_ref().unwrap()) + ) + } + } +} + +impl Debug for DataType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.0.is_null() { + write!(f, "null") + } else { + write!( + f, + "{}", + ffi::graphar::to_type_name(self.0.as_ref().unwrap()) + ) + } + } +} + +impl DataType { + pub fn value_type(&self) -> Self { + DataType(self.0.value_type().clone()) + } + + pub fn id(&self) -> Type { + self.0.id() + } + + pub fn bool() -> Self { + Self(ffi::graphar::boolean().clone()) + } + + pub fn int32() -> Self { + Self(ffi::graphar::int32().clone()) + } + + pub fn int64() -> Self { + Self(ffi::graphar::int64().clone()) + } + + pub fn float32() -> Self { + Self(ffi::graphar::float32().clone()) + } + + pub fn float64() -> Self { + Self(ffi::graphar::float64().clone()) + } + + pub fn string() -> Self { + Self(ffi::graphar::string().clone()) + } + + pub fn date() -> Self { + Self(ffi::graphar::date().clone()) + } + + pub fn timestamp() -> Self { + Self(ffi::graphar::timestamp().clone()) + } + + pub fn list(value_type: &DataType) -> Self { Review Comment: Actually, when the inner type is null, this function won't panic. Only when we try to deref this pointer, it will panic. see https://docs.rs/cxx/latest/src/cxx/shared_ptr.rs.html#260-275 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
