Skip to main content

Content

Trait Content 

Source
pub trait Content:
    Sized
    + Send
    + Sync {
    type Underlying: Clone + PartialEq + Debug + Send + Sync;

    // Required methods
    fn get_range(&self, range: Range<usize>) -> &[Self::Underlying];
    fn decode_str(src: &str) -> Cow<'_, [Self::Underlying]>;
    fn encode_bytes(bytes: &[Self::Underlying]) -> Cow<'_, str>;
    fn get_char_column(&self, column: usize, offset: usize) -> usize;
}
Expand description

Abstracts source code text representation across different encodings.

Content allows the same AST operations to work with different text encodings (UTF-8, UTF-16, etc.) by providing encoding/decoding operations and position calculations. Essential for cross-platform support.

§Type Parameters

  • Underlying - The basic unit type (u8 for UTF-8, u16 for UTF-16, etc.)

§Example

// Content trait abstracts encoding differences
let content = "Hello, world!";
let bytes = content.get_range(0..5);  // [72, 101, 108, 108, 111] for UTF-8
let column = content.get_char_column(0, 7); // Character position

Required Associated Types§

Source

type Underlying: Clone + PartialEq + Debug + Send + Sync

The underlying data type (u8, u16, char, etc.)

Required Methods§

Source

fn get_range(&self, range: Range<usize>) -> &[Self::Underlying]

Get a slice of the underlying data for the given byte range

Source

fn decode_str(src: &str) -> Cow<'_, [Self::Underlying]>

Convert a string to this content’s underlying representation.

Used during text replacement to ensure proper encoding.

Source

fn encode_bytes(bytes: &[Self::Underlying]) -> Cow<'_, str>

Convert underlying data back to a string.

Used to extract text content after transformations.

Source

fn get_char_column(&self, column: usize, offset: usize) -> usize

Calculate the character column position at a given byte offset.

Handles Unicode properly by computing actual character positions rather than byte positions.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Content for String

Source§

fn get_char_column(&self, _col: usize, offset: usize) -> usize

This is an O(n) operation optimized with SIMD. SIMD allows efficient processing of unusually long lines. Modest improvements for standard code lines (~100 chars)

Source§

type Underlying = u8

Source§

fn get_range(&self, range: Range<usize>) -> &[Self::Underlying]

Source§

fn decode_str(src: &str) -> Cow<'_, [Self::Underlying]>

Source§

fn encode_bytes(bytes: &[Self::Underlying]) -> Cow<'_, str>

Implementors§