Skip to content

Commit c5ab530

Browse files
committed
move serde impls to separate module
1 parent 978ab7c commit c5ab530

File tree

3 files changed

+106
-101
lines changed

3 files changed

+106
-101
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ use rustc_middle::ty::print::PrintTraitRefExt;
6161
use rustc_middle::ty::{self, TyCtxt};
6262
use rustc_span::symbol::{Symbol, sym};
6363
use rustc_span::{BytePos, DUMMY_SP, FileName, RealFileName};
64-
use serde::ser::SerializeSeq as _;
65-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
6664
use tracing::{debug, info};
6765

6866
pub(crate) use self::context::*;
@@ -425,102 +423,6 @@ impl IndexItemFunctionType {
425423
}
426424
}
427425

428-
impl Serialize for IndexItemFunctionType {
429-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
430-
where
431-
S: Serializer,
432-
{
433-
struct ParamNames<'a>(&'a [Option<Symbol>]);
434-
435-
impl<'a> Serialize for ParamNames<'a> {
436-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
437-
where
438-
S: Serializer,
439-
{
440-
serializer.collect_seq(
441-
self.0
442-
.iter()
443-
.map(|symbol| symbol.as_ref().map(Symbol::as_str).unwrap_or_default()),
444-
)
445-
}
446-
}
447-
448-
let mut seq = serializer.serialize_seq(Some(2))?;
449-
450-
let mut fn_type = String::new();
451-
self.write_to_string_without_param_names(&mut fn_type);
452-
seq.serialize_element(&fn_type)?;
453-
454-
seq.serialize_element(&ParamNames(&self.param_names))?;
455-
456-
seq.end()
457-
}
458-
}
459-
460-
impl<'de> Deserialize<'de> for IndexItemFunctionType {
461-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
462-
where
463-
D: Deserializer<'de>,
464-
{
465-
use serde::de::{self, SeqAccess};
466-
467-
#[derive(Deserialize)]
468-
struct Deserialized {
469-
#[serde(deserialize_with = "function_signature")]
470-
function_signature: IndexItemFunctionType,
471-
#[serde(deserialize_with = "param_names")]
472-
param_names: Vec<Option<Symbol>>,
473-
}
474-
475-
fn function_signature<'de, D: Deserializer<'de>>(
476-
deserializer: D,
477-
) -> Result<IndexItemFunctionType, D::Error> {
478-
String::deserialize(deserializer).map(|sig| {
479-
IndexItemFunctionType::read_from_string_without_param_names(sig.as_bytes()).0
480-
})
481-
}
482-
483-
fn param_names<'de, D: Deserializer<'de>>(
484-
deserializer: D,
485-
) -> Result<Vec<Option<Symbol>>, D::Error> {
486-
struct Visitor;
487-
488-
impl<'de> de::Visitor<'de> for Visitor {
489-
type Value = Vec<Option<Symbol>>;
490-
491-
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
492-
f.write_str("seq of param names")
493-
}
494-
495-
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
496-
where
497-
A: SeqAccess<'de>,
498-
{
499-
let mut param_names = Vec::with_capacity(seq.size_hint().unwrap_or_default());
500-
501-
while let Some(symbol) = seq.next_element::<String>()? {
502-
param_names.push(if symbol.is_empty() {
503-
None
504-
} else {
505-
Some(Symbol::intern(&symbol))
506-
});
507-
}
508-
509-
Ok(param_names)
510-
}
511-
}
512-
513-
deserializer.deserialize_seq(Visitor)
514-
}
515-
516-
let Deserialized { mut function_signature, param_names } =
517-
Deserialized::deserialize(deserializer)?;
518-
function_signature.param_names = param_names;
519-
520-
Ok(function_signature)
521-
}
522-
}
523-
524426
#[derive(Debug, Clone)]
525427
pub(crate) struct StylePath {
526428
/// The path to the theme

src/librustdoc/html/render/search_index.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
pub(crate) mod encode;
2+
mod serde;
23

34
use std::collections::BTreeSet;
45
use std::collections::hash_map::Entry;
56
use std::path::Path;
67

8+
use ::serde::de::{self, Deserializer, Error as _};
9+
use ::serde::ser::{SerializeSeq, Serializer};
10+
use ::serde::{Deserialize, Serialize};
711
use rustc_ast::join_path_syms;
812
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
913
use rustc_hir::attrs::AttributeKind;
@@ -12,9 +16,6 @@ use rustc_middle::ty::TyCtxt;
1216
use rustc_span::def_id::DefId;
1317
use rustc_span::sym;
1418
use rustc_span::symbol::{Symbol, kw};
15-
use serde::de::{self, Deserializer, Error as _};
16-
use serde::ser::{SerializeSeq, Serializer};
17-
use serde::{Deserialize, Serialize};
1819
use stringdex::internals as stringdex_internals;
1920
use thin_vec::ThinVec;
2021
use tracing::instrument;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use std::fmt::{self, Formatter};
2+
3+
use rustc_span::Symbol;
4+
use serde::de::{self, SeqAccess};
5+
use serde::ser::SerializeSeq as _;
6+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
7+
8+
use crate::html::render::IndexItemFunctionType;
9+
10+
impl Serialize for IndexItemFunctionType {
11+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
12+
where
13+
S: Serializer,
14+
{
15+
struct ParamNames<'a>(&'a [Option<Symbol>]);
16+
17+
impl<'a> Serialize for ParamNames<'a> {
18+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19+
where
20+
S: Serializer,
21+
{
22+
serializer.collect_seq(
23+
self.0
24+
.iter()
25+
.map(|symbol| symbol.as_ref().map(Symbol::as_str).unwrap_or_default()),
26+
)
27+
}
28+
}
29+
30+
let mut seq = serializer.serialize_seq(Some(2))?;
31+
32+
let mut fn_type = String::new();
33+
self.write_to_string_without_param_names(&mut fn_type);
34+
seq.serialize_element(&fn_type)?;
35+
36+
seq.serialize_element(&ParamNames(&self.param_names))?;
37+
38+
seq.end()
39+
}
40+
}
41+
42+
impl<'de> Deserialize<'de> for IndexItemFunctionType {
43+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
44+
where
45+
D: Deserializer<'de>,
46+
{
47+
#[derive(Deserialize)]
48+
struct Deserialized {
49+
#[serde(deserialize_with = "function_signature")]
50+
function_signature: IndexItemFunctionType,
51+
#[serde(deserialize_with = "param_names")]
52+
param_names: Vec<Option<Symbol>>,
53+
}
54+
55+
fn function_signature<'de, D: Deserializer<'de>>(
56+
deserializer: D,
57+
) -> Result<IndexItemFunctionType, D::Error> {
58+
String::deserialize(deserializer).map(|sig| {
59+
IndexItemFunctionType::read_from_string_without_param_names(sig.as_bytes()).0
60+
})
61+
}
62+
63+
fn param_names<'de, D: Deserializer<'de>>(
64+
deserializer: D,
65+
) -> Result<Vec<Option<Symbol>>, D::Error> {
66+
struct Visitor;
67+
68+
impl<'de> de::Visitor<'de> for Visitor {
69+
type Value = Vec<Option<Symbol>>;
70+
71+
fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
72+
f.write_str("seq of param names")
73+
}
74+
75+
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
76+
where
77+
A: SeqAccess<'de>,
78+
{
79+
let mut param_names = Vec::with_capacity(seq.size_hint().unwrap_or_default());
80+
81+
while let Some(symbol) = seq.next_element::<String>()? {
82+
param_names.push(if symbol.is_empty() {
83+
None
84+
} else {
85+
Some(Symbol::intern(&symbol))
86+
});
87+
}
88+
89+
Ok(param_names)
90+
}
91+
}
92+
93+
deserializer.deserialize_seq(Visitor)
94+
}
95+
96+
let Deserialized { mut function_signature, param_names } =
97+
Deserialized::deserialize(deserializer)?;
98+
function_signature.param_names = param_names;
99+
100+
Ok(function_signature)
101+
}
102+
}

0 commit comments

Comments
 (0)