Skip to content

Commit 0c59af7

Browse files
committed
serialize San,SanPlus,UciMove without alloc
1 parent b29fb0a commit 0c59af7

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ jobs:
1414
- "nightly"
1515
flags:
1616
- ""
17-
- "--features variant"
17+
- "--features variant,serde"
18+
- "--features nohash-hasher,variant"
1819
- "--features alloc"
19-
- "--features serde"
20+
- "--features alloc,variant,serde"
2021
- "--features std"
21-
- "--features alloc,variant"
2222
- "--features std,variant,serde"
23-
- "--features nohash-hasher,variant"
2423
include:
2524
- toolchain: "nightly"
2625
flags: "-Z minimal-versions --all-features"
@@ -53,5 +52,5 @@ jobs:
5352
with:
5453
toolchain: stable
5554
targets: thumbv6m-none-eabi
56-
- run: cargo check --target thumbv6m-none-eabi --no-default-features --features variant,nohash-hasher
57-
- run: cargo check --target thumbv6m-none-eabi --no-default-features --features alloc,variant,nohash-hasher
55+
- run: cargo check --target thumbv6m-none-eabi --no-default-features --features variant,nohash-hasher,serde
56+
- run: cargo check --target thumbv6m-none-eabi --no-default-features --features alloc,variant,nohash-hasher,serde

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ rust-version = "1.75" # remember to update test.yml
1313

1414
[features]
1515
default = ["std"]
16-
alloc = []
16+
alloc = ["serde?/alloc"]
1717
std = ["alloc", "btoi/std", "nohash-hasher?/std"]
1818
variant = []
1919
nohash-hasher = ["dep:nohash-hasher"]
20-
serde = ["dep:serde", "alloc"]
20+
serde = ["dep:serde"]
2121

2222
[[bench]]
2323
name = "benches"

src/fen.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,15 @@ impl Display for Fen {
580580
}
581581
}
582582

583-
#[cfg(feature = "serde")]
583+
#[cfg(all(feature = "serde", feature = "alloc"))]
584584
impl serde::Serialize for Fen {
585585
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
586586
where
587587
S: serde::Serializer,
588588
{
589-
serializer.serialize_str(&alloc::string::ToString::to_string(self))
589+
let mut s = alloc::string::String::new();
590+
self.append_to_string(&mut s);
591+
serializer.serialize_str(&s)
590592
}
591593
}
592594

@@ -701,13 +703,15 @@ impl Display for Epd {
701703
}
702704
}
703705

704-
#[cfg(feature = "serde")]
706+
#[cfg(all(feature = "serde", feature = "alloc"))]
705707
impl serde::Serialize for Epd {
706708
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
707709
where
708710
S: serde::Serializer,
709711
{
710-
serializer.serialize_str(&alloc::string::ToString::to_string(self))
712+
let mut s = alloc::string::String::new();
713+
self.append_to_string(&mut s);
714+
serializer.serialize_str(&s)
711715
}
712716
}
713717

src/san.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,10 @@ impl serde::Serialize for San {
549549
where
550550
S: serde::Serializer,
551551
{
552-
serializer.serialize_str(&alloc::string::ToString::to_string(self))
552+
// Longest syntactically valid SAN: Na1xa1=Q
553+
let mut s = arrayvec::ArrayString::<8>::new();
554+
let _ = self.append_to(&mut s);
555+
serializer.serialize_str(&s)
553556
}
554557
}
555558

@@ -725,7 +728,10 @@ impl serde::Serialize for SanPlus {
725728
where
726729
S: serde::Serializer,
727730
{
728-
serializer.serialize_str(&alloc::string::ToString::to_string(self))
731+
// Longest syntactically valid SAN with suffix: Na1xa1=Q#
732+
let mut s = arrayvec::ArrayString::<9>::new();
733+
let _ = self.append_to(&mut s);
734+
serializer.serialize_str(&s)
729735
}
730736
}
731737

src/uci.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ impl serde::Serialize for UciMove {
138138
where
139139
S: serde::Serializer,
140140
{
141-
serializer.serialize_str(&alloc::string::ToString::to_string(self))
141+
let mut s = arrayvec::ArrayString::<5>::new();
142+
let _ = self.append_to(&mut s);
143+
serializer.serialize_str(&s)
142144
}
143145
}
144146

src/util.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::{convert::TryFrom as _, fmt, fmt::Write as _, num::TryFromIntError};
22

3+
use arrayvec::ArrayString;
4+
35
pub(crate) fn out_of_range_error() -> TryFromIntError {
46
// This is a hack to construct TryFromIntError despite its private
57
// constructor. The standard library keeps it private intentionally,
@@ -67,6 +69,17 @@ impl AppendAscii for fmt::Formatter<'_> {
6769
}
6870
}
6971

72+
impl<const CAP: usize> AppendAscii for ArrayString<CAP> {
73+
type Error = core::convert::Infallible;
74+
75+
fn reserve(&mut self, _additional: usize) {}
76+
77+
fn append_ascii(&mut self, ascii_char: char) -> Result<(), Self::Error> {
78+
self.push(ascii_char);
79+
Ok(())
80+
}
81+
}
82+
7083
#[cfg(feature = "alloc")]
7184
impl AppendAscii for alloc::string::String {
7285
type Error = core::convert::Infallible;

0 commit comments

Comments
 (0)