Unnamed repository; edit this file 'description' to name the repository.
added serde to span
bit-aloo 4 months ago
parent c129572 · commit c7f8f6b
-rw-r--r--Cargo.lock2
-rw-r--r--crates/span/Cargo.toml1
-rw-r--r--crates/span/src/ast_id.rs3
-rw-r--r--crates/span/src/hygiene.rs5
-rw-r--r--crates/span/src/lib.rs23
5 files changed, 29 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 42eaeb01f1..453c35a574 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1881,6 +1881,7 @@ dependencies = [
"postcard",
"proc-macro-api",
"proc-macro-srv",
+ "span",
]
[[package]]
@@ -2648,6 +2649,7 @@ dependencies = [
"la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 2.1.1",
"salsa",
+ "serde",
"stdx",
"syntax",
"text-size 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/crates/span/Cargo.toml b/crates/span/Cargo.toml
index cfb319d688..d331f63d0c 100644
--- a/crates/span/Cargo.toml
+++ b/crates/span/Cargo.toml
@@ -21,6 +21,7 @@ text-size.workspace = true
vfs.workspace = true
syntax.workspace = true
stdx.workspace = true
+serde.workspace = true
[dev-dependencies]
syntax.workspace = true
diff --git a/crates/span/src/ast_id.rs b/crates/span/src/ast_id.rs
index 599b3c7175..37954ca0b0 100644
--- a/crates/span/src/ast_id.rs
+++ b/crates/span/src/ast_id.rs
@@ -29,6 +29,7 @@ use std::{
use la_arena::{Arena, Idx, RawIdx};
use rustc_hash::{FxBuildHasher, FxHashMap};
+use serde::{Deserialize, Serialize};
use syntax::{
AstNode, AstPtr, SyntaxKind, SyntaxNode, SyntaxNodePtr,
ast::{self, HasName},
@@ -54,7 +55,7 @@ pub const NO_DOWNMAP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
ErasedFileAstId(pack_hash_index_and_kind(0, 0, ErasedFileAstIdKind::NoDownmap as u32));
/// This is a type erased FileAstId.
-#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ErasedFileAstId(u32);
impl fmt::Debug for ErasedFileAstId {
diff --git a/crates/span/src/hygiene.rs b/crates/span/src/hygiene.rs
index 6805417177..1a82f221c6 100644
--- a/crates/span/src/hygiene.rs
+++ b/crates/span/src/hygiene.rs
@@ -21,11 +21,14 @@
//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
use std::fmt;
+#[cfg(feature = "salsa")]
+use serde::{Deserialize, Serialize};
+
use crate::Edition;
/// A syntax context describes a hierarchy tracking order of macro definitions.
#[cfg(feature = "salsa")]
-#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct SyntaxContext(
/// # Invariant
///
diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs
index bfe7b2620d..f6581de38c 100644
--- a/crates/span/src/lib.rs
+++ b/crates/span/src/lib.rs
@@ -20,6 +20,7 @@ pub use self::{
map::{RealSpanMap, SpanMap},
};
+use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub use syntax::Edition;
pub use text_size::{TextRange, TextSize};
pub use vfs::FileId;
@@ -68,11 +69,12 @@ impl Span {
/// Spans represent a region of code, used by the IDE to be able link macro inputs and outputs
/// together. Positions in spans are relative to some [`SpanAnchor`] to make them more incremental
/// friendly.
-#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Span {
/// The text range of this span, relative to the anchor.
/// We need the anchor for incrementality, as storing absolute ranges will require
/// recomputation on every change in a file at all times.
+ #[serde(serialize_with = "serialize_text_range", deserialize_with = "deserialize_text_range")]
pub range: TextRange,
/// The anchor this span is relative to.
pub anchor: SpanAnchor,
@@ -80,6 +82,21 @@ pub struct Span {
pub ctx: SyntaxContext,
}
+fn serialize_text_range<S>(range: &TextRange, serializer: S) -> Result<S::Ok, S::Error>
+where
+ S: Serializer,
+{
+ (u32::from(range.start()), u32::from(range.end())).serialize(serializer)
+}
+
+fn deserialize_text_range<'de, D>(deserializer: D) -> Result<TextRange, D::Error>
+where
+ D: Deserializer<'de>,
+{
+ let (start, end) = <(u32, u32)>::deserialize(deserializer)?;
+ Ok(TextRange::new(TextSize::from(start), TextSize::from(end)))
+}
+
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
@@ -112,7 +129,7 @@ impl fmt::Display for Span {
}
}
-#[derive(Copy, Clone, PartialEq, Eq, Hash)]
+#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SpanAnchor {
pub file_id: EditionedFileId,
pub ast_id: ErasedFileAstId,
@@ -126,7 +143,7 @@ impl fmt::Debug for SpanAnchor {
/// A [`FileId`] and [`Edition`] bundled up together.
/// The MSB is reserved for `HirFileId` encoding, more upper bits are used to then encode the edition.
-#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EditionedFileId(u32);
impl fmt::Debug for EditionedFileId {