Skip to main content

Chunk

Struct Chunk 

Source
pub struct Chunk {
    pub id: Option<i64>,
    pub buffer_id: i64,
    pub content: String,
    pub byte_range: Range<usize>,
    pub index: usize,
    pub metadata: ChunkMetadata,
}
Expand description

Represents a chunk of text from a buffer.

Chunks are created by chunking strategies and contain a portion of buffer content along with metadata about their position and origin.

§Examples

use rlm_rs::core::Chunk;

let chunk = Chunk::new(
    1,
    "Hello, world!".to_string(),
    0..13,
    0,
);
assert_eq!(chunk.size(), 13);

Fields§

§id: Option<i64>

Unique identifier (assigned by storage layer).

§buffer_id: i64

ID of the buffer this chunk belongs to.

§content: String

Chunk content.

§byte_range: Range<usize>

Byte range in the original buffer.

§index: usize

Sequential index within the buffer (0-based).

§metadata: ChunkMetadata

Chunk metadata.

Implementations§

Source§

impl Chunk

Source

pub fn new( buffer_id: i64, content: String, byte_range: Range<usize>, index: usize, ) -> Self

Creates a new chunk.

§Arguments
  • buffer_id - ID of the parent buffer.
  • content - Chunk content.
  • byte_range - Byte range in the original buffer.
  • index - Sequential index within the buffer.
§Examples
use rlm_rs::core::Chunk;

let chunk = Chunk::new(1, "content".to_string(), 0..7, 0);
assert_eq!(chunk.buffer_id, 1);
assert_eq!(chunk.index, 0);
Source

pub fn with_strategy( buffer_id: i64, content: String, byte_range: Range<usize>, index: usize, strategy: &str, ) -> Self

Creates a chunk with a specific strategy name.

§Arguments
  • buffer_id - ID of the parent buffer.
  • content - Chunk content.
  • byte_range - Byte range in the original buffer.
  • index - Sequential index within the buffer.
  • strategy - Name of the chunking strategy.
Source

pub const fn size(&self) -> usize

Returns the size of the chunk in bytes.

Source

pub const fn range_size(&self) -> usize

Returns the byte range size.

Source

pub const fn is_empty(&self) -> bool

Checks if the chunk is empty.

Source

pub const fn start(&self) -> usize

Returns the start byte offset in the original buffer.

Source

pub const fn end(&self) -> usize

Returns the end byte offset in the original buffer.

Source

pub const fn set_token_count(&mut self, count: usize)

Sets the token count estimate.

Source

pub const fn estimate_tokens(&self) -> usize

Estimates token count using a simple heuristic.

Uses the approximation of ~4 characters per token for ASCII text. For a more accurate estimate, use Self::estimate_tokens_accurate.

§Accuracy

This simple method is typically accurate within 20-30% for English text and code. It tends to undercount for text with many short words and overcount for text with long technical terms.

Source

pub fn estimate_tokens_accurate(&self) -> usize

Estimates token count with improved accuracy.

Uses a more sophisticated heuristic that accounts for:

  • Word boundaries (whitespace-separated tokens)
  • Punctuation and operators (often separate tokens)
  • Non-ASCII characters (typically 1-2 chars per token)
§Accuracy

This method is typically accurate within 10-15% for mixed content. For production use requiring exact counts, consider integrating a proper tokenizer like tiktoken-rs.

§Performance

This method iterates over the content string, so it’s O(n) where n is the content length. For very large chunks, the simple Self::estimate_tokens method may be preferred.

Source

pub const fn set_line_range(&mut self, start_line: usize, end_line: usize)

Sets the line range in the original buffer.

Source

pub const fn set_has_overlap(&mut self, has_overlap: bool)

Marks this chunk as having overlap with the previous chunk.

Source

pub fn compute_hash(&mut self)

Computes and sets the content hash.

Source

pub fn preview(&self, max_len: usize) -> &str

Returns a preview of the chunk content (first N characters).

§Arguments
  • max_len - Maximum number of characters to include.
Source

pub const fn overlaps_with(&self, other_range: &Range<usize>) -> bool

Checks if this chunk’s byte range overlaps with another range.

Source

pub fn contains_offset(&self, offset: usize) -> bool

Checks if this chunk’s byte range contains a specific byte offset.

Trait Implementations§

Source§

impl Clone for Chunk

Source§

fn clone(&self) -> Chunk

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Chunk

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Chunk

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Chunk

Source§

fn eq(&self, other: &Chunk) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Chunk

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Chunk

Source§

impl StructuralPartialEq for Chunk

Auto Trait Implementations§

§

impl Freeze for Chunk

§

impl RefUnwindSafe for Chunk

§

impl Send for Chunk

§

impl Sync for Chunk

§

impl Unpin for Chunk

§

impl UnwindSafe for Chunk

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,