chore: add len() to Bytes and StringBytes (#1455)

* chore: add `len()` to Bytes and StringBytes

* chore: add `len()` to Bytes and StringBytes
This commit is contained in:
shuiyisong
2023-04-25 10:18:41 +08:00
committed by GitHub
parent b9db2cfd83
commit 69acf32914
2 changed files with 40 additions and 36 deletions

View File

@@ -20,6 +20,16 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
pub struct Bytes(bytes::Bytes);
impl Bytes {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<Bytes> for bytes::Bytes {
fn from(value: Bytes) -> Self {
value.0
@@ -92,6 +102,14 @@ impl StringBytes {
pub fn as_utf8(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(&self.0) }
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<String> for StringBytes {
@@ -178,6 +196,17 @@ mod tests {
assert_eq!(world, &bytes);
}
#[test]
fn test_bytes_len() {
let hello = b"hello".to_vec();
let bytes = Bytes::from(hello.clone());
assert_eq!(bytes.len(), hello.len());
let zero = b"".to_vec();
let bytes = Bytes::from(zero);
assert!(bytes.is_empty());
}
#[test]
fn test_string_bytes_from() {
let hello = "hello".to_string();
@@ -191,6 +220,17 @@ mod tests {
assert_eq!(&bytes, world);
}
#[test]
fn test_string_bytes_len() {
let hello = "hello".to_string();
let bytes = StringBytes::from(hello.clone());
assert_eq!(bytes.len(), hello.len());
let zero = "".to_string();
let bytes = StringBytes::from(zero);
assert!(bytes.is_empty());
}
fn check_str(expect: &str, given: &str) {
assert_eq!(expect, given);
}

View File

@@ -1,36 +0,0 @@
// Copyright 2023 Greptime Team
//
// Licensed 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::sync::Arc;
use promql_parser::parser::Value;
use crate::error::Result;
mod evaluator;
mod functions;
pub use evaluator::*;
pub struct Context {}
pub struct Query {}
pub struct Engine {}
impl Engine {
pub fn exec(_ctx: &Context, _q: Query) -> Result<Arc<dyn Value>> {
unimplemented!();
}
}