pub struct Source { /* private fields */ }
Expand description
源代码表示结构。
统一的源代码抽象,支持从文件、字符串等多种来源加载代码, 并提供完整的源码位置追踪和错误报告能力。
§设计特点
§内存共享
使用 Arc<Vec<char>>
来共享源码内容,避免重复拷贝,
多个 Token 和 AST 节点可以安全地引用同一份源码。
§路径追踪
可选的文件路径信息,用于错误报告和调试时显示文件来源。
§Unicode 友好
内部使用 Vec<char>
存储,完整支持 Unicode 字符,
便于处理多语言源码和字符边界计算。
§序列化支持
实现了 Serde 序列化,支持 AST 的持久化存储和网络传输。
§使用示例
ⓘ
// 从字符串创建
let source = Source::from_string("let x = 42".to_string());
// 从文件创建
let source = Source::from_file("example.onion")?;
// 带路径信息创建
let source = Source::from_string_with_file_path(
"let y = x + 1".to_string(),
"inline.onion"
);
Implementations§
Source§impl Source
impl Source
Sourcepub fn from_string(source: String) -> Self
pub fn from_string(source: String) -> Self
Sourcepub fn from_string_with_file_path<P: AsRef<Path>>(
source: String,
path: P,
) -> Self
pub fn from_string_with_file_path<P: AsRef<Path>>( source: String, path: P, ) -> Self
Sourcepub fn content_str(&self) -> String
pub fn content_str(&self) -> String
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Source
impl<'de> Deserialize<'de> for Source
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
从字符串反序列化 Source。
将序列化的字符串转换回 Source 结构,文件路径信息设置为 None
,
因为序列化时未包含路径信息。
Auto Trait Implementations§
impl Freeze for Source
impl RefUnwindSafe for Source
impl Send for Source
impl Sync for Source
impl Unpin for Source
impl UnwindSafe for Source
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more