pub struct DuckStringView<'a> { /* private fields */ }Expand description
A parsed view of a duckdb_string_t value.
This type borrows from the raw vector data — it does not allocate.
§Constructing a view
A duckdb_string_t longer than DUCK_STRING_INLINE_MAX_LEN stores a raw
heap pointer in bytes 8–15. Reading such a value means dereferencing a
pointer taken from the input bytes, which can only be justified by the
caller — so it lives behind from_raw, an unsafe fn.
inline_from_bytes is the safe constructor: it
accepts any 16 bytes and refuses (returns None) whenever the value is in
pointer format, so a view built through it can never dereference anything.
Implementations§
Source§impl<'a> DuckStringView<'a>
impl<'a> DuckStringView<'a>
Sourcepub const unsafe fn from_raw(raw: &'a [u8; 16]) -> Self
pub const unsafe fn from_raw(raw: &'a [u8; 16]) -> Self
Creates a DuckStringView from a genuine duckdb_string_t.
This is the constructor to use inside DuckDB callbacks, where the 16
bytes really did come from a DuckDB vector.
§Safety
If the length field (bytes 0–3) exceeds DUCK_STRING_INLINE_MAX_LEN,
bytes 8–15 must hold a valid pointer to at least length readable bytes
that stay live for 'a.
Sourcepub const fn inline_from_bytes(raw: &'a [u8; 16]) -> Option<Self>
pub const fn inline_from_bytes(raw: &'a [u8; 16]) -> Option<Self>
Creates a DuckStringView from 16 bytes that may come from anywhere.
Returns None if the value is in pointer format (length >
DUCK_STRING_INLINE_MAX_LEN), because honouring the embedded pointer
would mean dereferencing an address chosen by the input.
§Example
use quack_rs::vector::string::DuckStringView;
let mut inline = [0u8; 16];
inline[..4].copy_from_slice(&5u32.to_ne_bytes());
inline[4..9].copy_from_slice(b"hello");
assert_eq!(
DuckStringView::inline_from_bytes(&inline).and_then(|v| v.as_str()),
Some("hello")
);
// A pointer-format value is refused rather than dereferenced.
let mut pointer_format = [0u8; 16];
pointer_format[..4].copy_from_slice(&64u32.to_ne_bytes());
pointer_format[8..16].copy_from_slice(&0xdead_beef_u64.to_ne_bytes());
assert!(DuckStringView::inline_from_bytes(&pointer_format).is_none());Sourcepub const fn from_bytes(raw: &'a [u8; 16]) -> Self
👎Deprecated since 0.16.0: use DuckStringView::from_raw (unsafe, honours pointer format) or DuckStringView::inline_from_bytes (safe, refuses pointer format)
pub const fn from_bytes(raw: &'a [u8; 16]) -> Self
use DuckStringView::from_raw (unsafe, honours pointer format) or DuckStringView::inline_from_bytes (safe, refuses pointer format)
Creates a DuckStringView from the raw 16-byte representation.
§Deprecated
This constructor is safe but cannot honour pointer-format values: doing
so would dereference an address supplied by its (safe) caller. It now
behaves exactly like inline_from_bytes
except that it cannot report the refusal, so a pointer-format value
yields a view whose accessors return None.
Use from_raw for real duckdb_string_t values, or
inline_from_bytes when the input is
untrusted and you want the refusal to be visible.
Sourcepub fn as_str(&self) -> Option<&'a str>
pub fn as_str(&self) -> Option<&'a str>
Returns the string as a UTF-8 str slice, or None if it is not valid UTF-8.
The returned &'a str has the same lifetime as the underlying data slice —
not the lifetime of self. This allows the result to outlive the DuckStringView.
Returns None when the bytes are not valid UTF-8, when a pointer-format
value carries a null pointer, or when this view was built through a safe
constructor and the value is in pointer format (see
inline_from_bytes).
The pointer-format obligation — that bytes 8–15 address len() live
bytes — was discharged when the view was created via
from_raw.
Trait Implementations§
Source§impl<'a> Clone for DuckStringView<'a>
impl<'a> Clone for DuckStringView<'a>
Source§fn clone(&self) -> DuckStringView<'a>
fn clone(&self) -> DuckStringView<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more