Skip to main content

DuckStringView

Struct DuckStringView 

Source
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>

Source

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.

Source

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());
Source

pub 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)

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.

Source

pub const fn len(&self) -> usize

Returns the length of the string in bytes.

Source

pub const fn is_empty(&self) -> bool

Returns true if the string is empty.

Source

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>

Source§

fn clone(&self) -> DuckStringView<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for DuckStringView<'a>

Source§

impl<'a> Debug for DuckStringView<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for DuckStringView<'a>

§

impl<'a> RefUnwindSafe for DuckStringView<'a>

§

impl<'a> Send for DuckStringView<'a>

§

impl<'a> Sync for DuckStringView<'a>

§

impl<'a> Unpin for DuckStringView<'a>

§

impl<'a> UnsafeUnpin for DuckStringView<'a>

§

impl<'a> UnwindSafe for DuckStringView<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.