Skip to content

Commit

Permalink
Format doctests using rustfmt's format_code_in_doc_comments
Browse files Browse the repository at this point in the history
    cargo fmt -- --config format_code_in_doc_comments=true
  • Loading branch information
dtolnay committed Jul 6, 2023
1 parent 290449f commit 3bfd41d
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 153 deletions.
15 changes: 7 additions & 8 deletions serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,10 @@ impl<'a> fmt::Display for Unexpected<'a> {
/// # E: de::Error,
/// # {
/// # let v = true;
/// return Err(de::Error::invalid_type(Unexpected::Bool(v), &"a negative integer"));
/// return Err(de::Error::invalid_type(
/// Unexpected::Bool(v),
/// &"a negative integer",
/// ));
/// # }
/// ```
pub trait Expected {
Expand Down Expand Up @@ -709,7 +712,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
/// {
/// // Decrease the number of reallocations if there are many elements
/// if let Some(size_hint) = seq.size_hint() {
/// self.0.reserve(size_hint);
/// self.0.reserve(size_hint);
/// }
///
/// // Visit each element in the inner array and push it onto
Expand Down Expand Up @@ -2148,11 +2151,7 @@ pub trait VariantAccess<'de>: Sized {
/// # T: DeserializeSeed<'de>,
/// # { unimplemented!() }
/// #
/// fn tuple_variant<V>(
/// self,
/// _len: usize,
/// _visitor: V,
/// ) -> Result<V::Value, Self::Error>
/// fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
/// where
/// V: Visitor<'de>,
/// {
Expand Down Expand Up @@ -2239,9 +2238,9 @@ pub trait VariantAccess<'de>: Sized {
/// # Example
///
/// ```edition2021
/// use std::str::FromStr;
/// use serde::de::{value, Deserialize, IntoDeserializer};
/// use serde_derive::Deserialize;
/// use std::str::FromStr;
///
/// #[derive(Deserialize)]
/// enum Setting {
Expand Down
4 changes: 2 additions & 2 deletions serde/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! trait.
//!
//! ```edition2021
//! use std::str::FromStr;
//! use serde_derive::Deserialize;
//! use serde::de::{value, Deserialize, IntoDeserializer};
//! use serde_derive::Deserialize;
//! use std::str::FromStr;
//!
//! #[derive(Deserialize)]
//! enum Setting {
Expand Down
22 changes: 9 additions & 13 deletions serde/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ pub trait Serializer: Sized {
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
/// use serde::ser::{Serialize, SerializeSeq, Serializer};
///
/// impl<T> Serialize for Vec<T>
/// where
Expand All @@ -995,7 +995,7 @@ pub trait Serializer: Sized {
/// then a call to `end`.
///
/// ```edition2021
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// # mod fool {
/// # trait Serialize {}
Expand Down Expand Up @@ -1150,7 +1150,7 @@ pub trait Serializer: Sized {
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeMap};
/// use serde::ser::{Serialize, SerializeMap, Serializer};
///
/// impl<K, V> Serialize for HashMap<K, V>
/// where
Expand Down Expand Up @@ -1370,9 +1370,7 @@ pub trait Serializer: Sized {
/// where
/// S: Serializer,
/// {
/// serializer.collect_str(&format_args!("{:?}{:?}",
/// self.naive_local(),
/// self.offset()))
/// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset()))
/// }
/// }
/// ```
Expand Down Expand Up @@ -1408,9 +1406,7 @@ pub trait Serializer: Sized {
/// where
/// S: Serializer,
/// {
/// serializer.collect_str(&format_args!("{:?}{:?}",
/// self.naive_local(),
/// self.offset()))
/// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset()))
/// }
/// }
/// ```
Expand Down Expand Up @@ -1496,7 +1492,7 @@ pub trait Serializer: Sized {
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
/// use serde::ser::{Serialize, SerializeSeq, Serializer};
///
/// impl<T> Serialize for Vec<T>
/// where
Expand Down Expand Up @@ -1542,7 +1538,7 @@ pub trait SerializeSeq {
/// # Example use
///
/// ```edition2021
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// # mod fool {
/// # trait Serialize {}
Expand Down Expand Up @@ -1590,7 +1586,7 @@ pub trait SerializeSeq {
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
///
/// # mod fool {
/// # trait Serialize {}
Expand Down Expand Up @@ -1764,7 +1760,7 @@ pub trait SerializeTupleVariant {
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeMap};
/// use serde::ser::{Serialize, SerializeMap, Serializer};
///
/// impl<K, V> Serialize for HashMap<K, V>
/// where
Expand Down
69 changes: 42 additions & 27 deletions serde_test/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ use std::fmt::Debug;
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// assert_tokens(
/// &s,
/// &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ],
/// );
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
Expand All @@ -50,14 +53,17 @@ where
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_ser_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// assert_ser_tokens(
/// &s,
/// &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ],
/// );
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_ser_tokens<T: ?Sized>(value: &T, tokens: &[Token])
Expand Down Expand Up @@ -91,7 +97,9 @@ where
/// }
///
/// fn main() {
/// let example = Example { lock: Arc::new(Mutex::new(0)) };
/// let example = Example {
/// lock: Arc::new(Mutex::new(0)),
/// };
/// let lock = example.lock.clone();
///
/// let _ = thread::spawn(move || {
Expand All @@ -102,10 +110,14 @@ where
/// // This panic while holding the lock (`_guard` is in scope) will
/// // poison the mutex.
/// panic!()
/// }).join();
/// })
/// .join();
///
/// let expected = &[
/// Token::Struct { name: "Example", len: 1 },
/// Token::Struct {
/// name: "Example",
/// len: 1,
/// },
/// Token::Str("lock"),
/// ];
/// let error = "lock poison error while serializing";
Expand Down Expand Up @@ -141,14 +153,17 @@ where
/// }
///
/// let s = S { a: 0, b: 0 };
/// assert_de_tokens(&s, &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ]);
/// assert_de_tokens(
/// &s,
/// &[
/// Token::Struct { name: "S", len: 2 },
/// Token::Str("a"),
/// Token::U8(0),
/// Token::Str("b"),
/// Token::U8(0),
/// Token::StructEnd,
/// ],
/// );
/// ```
#[cfg_attr(not(no_track_caller), track_caller)]
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
Expand Down
38 changes: 21 additions & 17 deletions serde_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! # const IGNORE: &str = stringify! {
//! use linked_hash_map::LinkedHashMap;
//! # };
//! use serde_test::{Token, assert_tokens};
//! use serde_test::{assert_tokens, Token};
//!
//! # use std::fmt;
//! # use std::marker::PhantomData;
Expand Down Expand Up @@ -106,10 +106,13 @@
//! fn test_ser_de_empty() {
//! let map = LinkedHashMap::<char, u32>::new();
//!
//! assert_tokens(&map, &[
//! Token::Map { len: Some(0) },
//! Token::MapEnd,
//! ]);
//! assert_tokens(
//! &map,
//! &[
//! Token::Map { len: Some(0) },
//! Token::MapEnd,
//! ],
//! );
//! }
//!
//! #[test]
Expand All @@ -120,18 +123,19 @@
//! map.insert('a', 10);
//! map.insert('c', 30);
//!
//! assert_tokens(&map, &[
//! Token::Map { len: Some(3) },
//! Token::Char('b'),
//! Token::I32(20),
//!
//! Token::Char('a'),
//! Token::I32(10),
//!
//! Token::Char('c'),
//! Token::I32(30),
//! Token::MapEnd,
//! ]);
//! assert_tokens(
//! &map,
//! &[
//! Token::Map { len: Some(3) },
//! Token::Char('b'),
//! Token::I32(20),
//! Token::Char('a'),
//! Token::I32(10),
//! Token::Char('c'),
//! Token::I32(30),
//! Token::MapEnd,
//! ],
//! );
//! }
//! #
//! # fn main() {
Expand Down
Loading

0 comments on commit 3bfd41d

Please sign in to comment.