• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16728097825

04 Aug 2025 04:00PM UTC coverage: 48.355% (-35.1%) from 83.429%
16728097825

Pull #4108

github

web-flow
Merge 1b2d27fd8 into 649ba9576
Pull Request #4108: perf[vortex-array]: use all_valid instead of `invalid_count() == 0`

1 of 1 new or added line in 1 file covered. (100.0%)

11596 existing lines in 378 files now uncovered.

18635 of 38538 relevant lines covered (48.35%)

151786.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.79
/vortex-buffer/src/string.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::fmt::{Debug, Formatter};
5
use std::ops::Deref;
6
use std::str::Utf8Error;
7

8
use crate::ByteBuffer;
9

10
/// A wrapper around a [`ByteBuffer`] that guarantees that the buffer contains valid UTF-8.
11
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12
pub struct BufferString(ByteBuffer);
13

14
impl BufferString {
15
    /// Creates a new `BufferString` from a [`ByteBuffer`].
16
    ///
17
    /// # Safety
18
    /// Assumes that the buffer contains valid UTF-8.
19
    pub const unsafe fn new_unchecked(buffer: ByteBuffer) -> Self {
72✔
20
        Self(buffer)
72✔
21
    }
72✔
22

23
    /// Return a view of the contents of BufferString as an immutable `&str`.
24
    pub fn as_str(&self) -> &str {
4,968✔
25
        // SAFETY: We have already validated that the buffer is valid UTF-8
26
        unsafe { std::str::from_utf8_unchecked(self.0.as_ref()) }
4,968✔
27
    }
4,968✔
28

29
    /// Returns the inner [`ByteBuffer`].
30
    pub fn into_inner(self) -> ByteBuffer {
152✔
31
        self.0
152✔
32
    }
152✔
33

34
    /// Returns reference to the inner [`ByteBuffer`].
35
    pub fn inner(&self) -> &ByteBuffer {
72✔
36
        &self.0
72✔
37
    }
72✔
38
}
39

40
impl Debug for BufferString {
41
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
42
        f.debug_struct("BufferString")
×
43
            .field("string", &self.as_str())
×
44
            .finish()
×
45
    }
×
46
}
47

48
impl From<BufferString> for ByteBuffer {
49
    fn from(value: BufferString) -> Self {
×
50
        value.0
×
51
    }
×
52
}
53

54
impl From<String> for BufferString {
55
    fn from(value: String) -> Self {
3,314✔
56
        Self(ByteBuffer::from(value.into_bytes()))
3,314✔
57
    }
3,314✔
58
}
59

60
impl From<&str> for BufferString {
61
    fn from(value: &str) -> Self {
242✔
62
        Self(ByteBuffer::from(String::from(value).into_bytes()))
242✔
63
    }
242✔
64
}
65

66
impl TryFrom<ByteBuffer> for BufferString {
67
    type Error = Utf8Error;
68

69
    fn try_from(value: ByteBuffer) -> Result<Self, Self::Error> {
454✔
70
        let _ = std::str::from_utf8(value.as_ref())?;
454✔
71
        Ok(Self(value))
454✔
72
    }
454✔
73
}
74

75
impl Deref for BufferString {
76
    type Target = str;
77

78
    fn deref(&self) -> &Self::Target {
2,714✔
79
        self.as_str()
2,714✔
80
    }
2,714✔
81
}
82

83
impl AsRef<str> for BufferString {
UNCOV
84
    fn as_ref(&self) -> &str {
×
UNCOV
85
        self.as_str()
×
UNCOV
86
    }
×
87
}
88

89
impl AsRef<[u8]> for BufferString {
90
    fn as_ref(&self) -> &[u8] {
2,104✔
91
        self.as_str().as_bytes()
2,104✔
92
    }
2,104✔
93
}
94

95
#[cfg(test)]
96
mod test {
97
    use crate::{Alignment, BufferString, buffer};
98

99
    #[test]
100
    fn buffer_string() {
101
        let buf = BufferString::from("hello");
102
        assert_eq!(buf.len(), 5);
103
        assert_eq!(buf.into_inner().alignment(), Alignment::of::<u8>());
104
    }
105

106
    #[test]
107
    fn buffer_string_non_ut8() {
108
        assert!(BufferString::try_from(buffer![0u8, 255]).is_err());
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc