Repository navigation
Experiment with replacing specialization in libcore with downcasting - #165340
Draft
frank-king wants to merge 5 commits into
Draft
frank-king wants to merge 5 commits into
frank-king wants to merge 5 commits into
Conversation
- refactors `alloc::vec::spec_from_elem::SpecFromElem`.
Contributor
Author
|
(WIP)
|
| Trait | Replacable by try_as_dyn{_mut} Or <dyn Any>::downcast{_ref|_mut}? |
|---|---|
alloc::collections::vec_deque::SpecExtendFromWithin |
Yes |
alloc::collections::vec_deque::spec_extend::{SpecExtend, SpecExtendFront} |
No, cast by value |
alloc::collections::vec_deque::spec_from_iter::SpecFromIter |
No, cast by value |
alloc::collections::SpecExtend |
Yes |
alloc::ffi::c_str::CStringt::new::SpecNewImpl |
Yes |
alloc::io::copy::specialization::SpecCopyInner |
No, SpecCopy not dyn compatible |
alloc::io::util::SpecReadByte |
No, cannot downcast from R: Read to BufReader<R: Read> |
alloc::vec::in_place_collect::SpecInPlaceCollect |
No, TrustedRandomAccessNoCoerce: Sized not dyn compatible |
alloc::vec::spec_extend_with::SpecExtendWith |
No, TrivialClone not dyn compatible |
alloc::vec::spec_extend::SpecExtend |
No, TrivialClone not dyn compatible |
alloc::vec::spec_from_elem::SpecFromElem |
Yes |
alloc::collections::vec_deque::SpecExtendFromWithin
// mod alloc::collections::vec_deque;
trait SpecExtendFromWithin {
unsafe fn spec_extend_from_within(&mut self, src: Range<usize>);
unsafe fn spec_prepend_from_within(&mut self, src: Range<usize>);
}
// default impl
impl<T: Clone, A: Allocator> SpecExtendFromWithin for VecDeque<T, A> { /* ... */ }
// specializations
impl<T: TrivialClone, A: Allocator> SpecExtendFromWithin for VecDeque<T, A> { /* ... */ }alloc::collections::vec_deque::spec_extend::{SpecExtend, SpecExtendFront}
// mod alloc::collections::vec_deque::spec_extend;
pub(super) trait SpecExtend<T, I> {
fn spec_extend(&mut self, iter: I);
}
// default impl
impl<T, I: Iterator<Item = T>, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> { /* ... */ }
// specializations
impl<T, I: TrustedLen<Item = T>, A: Allocator> SpecExtend<T, I> for VecDeque<T, A> { /* ... */ }
impl<T, A1: Allocator, A2: Allocator> SpecExtend<T, vec::IntoIter<T, A2>> for VecDeque<T, A1> { /* ... */ }
impl<'a, T: 'a + Copy, I: Iterator<Item = &'a T>, A: Allocator> SpecExtend<&'a T, I> for VecDeque<T, A> { /* ... */ }
impl<'a, T: 'a + Copy, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A> { /* ... */ }
pub(super) trait SpecExtendFront<T, I> {
fn spec_extend_front(&mut self, iter: I);
}
// default impl
impl<T, I: Iterator<Item = T>, A: Allocator> SpecExtendFront<T, I> for VecDeque<T, A> { /* ... */ }
// specializations
impl<T, A1: Allocator, A2: Allocator> SpecExtendFront<T, vec::IntoIter<T, A2>> for VecDeque<T, A1> { /* ... */ }
impl<T, A1: Allocator, A2: Allocator> SpecExtendFront<T, Rev<vec::IntoIter<T, A2>>> for VecDeque<T, A1> { /* ... */ }
impl<'a, T, A: Allocator> SpecExtendFront<T, Copied<slice::Iter<'a, T>>> for VecDeque<T, A>
where
Copied<slice::Iter<'a, T>>: Iterator<Item = T>,
{ /* ... */ }
impl<'a, T, A: Allocator> SpecExtendFront<T, Rev<Copied<slice::Iter<'a, T>>>> for VecDeque<T, A>
where
Rev<Copied<slice::Iter<'a, T>>>: Iterator<Item = T>,
{ /* ... */ }
impl<'a, T, A1: Allocator, A2: Allocator> SpecExtendFront<T, Drain<'a, T, A2>> for VecDeque<T, A1> { /* ... */ }
impl<'a, T, A1: Allocator, A2: Allocator> SpecExtendFront<T, Rev<Drain<'a, T, A2>>> for VecDeque<T, A1> { /* ... */ }alloc::collections::vec_deque::spec_from_iter::SpecFromIter
// mod alloc::collections::vec_deque::spec_from_iter;
pub(super) trait SpecFromIter<T, I> {
fn spec_from_iter(iter: I) -> Self;
}
// default impl
impl<T, I: Iterator<Item = T>> SpecFromIter<T, I> for VecDeque<T> { /* ... */ }
// specializations
impl<T> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T> { /* ... */ }
impl<T> SpecFromIter<T, IntoIter<T>> for VecDeque<T> { /* ... */ }alloc::collections::SpecExtend
// mod alloc::collections;
trait SpecExtend<I: IntoIterator> {
/// Extends `self` with the contents of the given iterator.
fn spec_extend(&mut self, iter: I);
}
// default impl
impl<I: IntoIterator, A: Allocator> SpecExtend<I> for LinkedList<I::Item, A> { /* ... */ }
// specializations
impl<T> SpecExtend<LinkedList<T>> for LinkedList<T> { /* ... */ }alloc::ffi::c_str::CStringt::new::SpecNewImpl
// mod alloc::ffi::c_str;
trait SpecNewImpl {
fn spec_new_impl(self) -> Result<CString, NulError>;
}
// default impl
impl<T: Into<Vec<u8>>> SpecNewImpl for T { /* ... */ }
// specializations
impl SpecNewImpl for &'_ [u8] { /* ... */ }
impl SpecNewImpl for &'_ str { /* ... */ }
impl SpecNewImpl for &'_ mut [u8] { /* ... */ }alloc::io::copy::specialization::SpecCopyInner
// mod alloc::io::copy::specialization;
pub(super) trait SpecCopyInner {
/// The implementation of `io::copy` that can rely on platform specific specialization
/// provided by `libstd`.
fn copy<W: Write + ?Sized>(&mut self, writer: &mut W) -> Result<CopyState>;
}
// default impl
impl<R: Read + ?Sized> SpecCopyInner for R { /* ... */ }
// specializations
impl<R: SpecCopy> SpecCopyInner for R { /* ... */ }
pub trait SpecCopy: Read {
fn copy<R: Read + ?Sized, W: Write + ?Sized>(
_reader: &mut R,
_writer: &mut W,
) -> Result<CopyState>;
}
impl<T: SpecCopy> SpecCopy for &mut T { /* ... */ }
impl<T: SpecCopy> SpecCopy for Take<T> { /* ... */ }
impl<T: ?Sized + SpecCopy> SpecCopy for BufReader<T> { /* ... */ }
</p>
</details>
<details><summary><code>alloc::io::util::SpecReadByte</code></summary>
<p>
```rust
// mod alloc::io::util;
pub trait SpecReadByte {
fn spec_read_byte(&mut self) -> Option<Result<u8>>;
}
// default impl
impl<R: Read> SpecReadByte for R { /* ... */ }
// specializations
impl SpecReadByte for StdinLock<'_> { /* ... */ }
impl<R: Read> SpecReadByte for BufReader<R> { /* ... */ }alloc::vec::in_place_collect::SpecInPlaceCollect
// mod alloc::vec::in_place_collect;
trait SpecInPlaceCollect<T, I>: Iterator<Item = T> {
unsafe fn collect_in_place(&mut self, dst: *mut T, end: *const T) -> usize;
}
// default impl
impl<T, I: Iterator<Item = T>> SpecInPlaceCollect<T, I> for I { /* ... */ }
// specializations
impl<T, I: Iterator<Item = T> + TrustedRandomAccessNoCoerce> SpecInPlaceCollect<T, I> for I { /* ... */ }alloc::vec::spec_extend_with::SpecExtendWith
// mod alloc::vec::spec_extend_with;
pub(super) trait SpecExtendWith<T> {
fn spec_extend_with(&mut self, n: usize, value: T);
}
// default impl
impl<T: Clone, A: Allocator> SpecExtendWith<T> for Vec<T, A> { /* ... */
// specialization
impl<T: TrivialClone, A: Allocator> SpecExtendWith<T> for Vec<T, A> { /* ... */ }
</p>
</details>
<details><summary><code>alloc::vec::spec_extend::SpecExtend</code></summary>
<p>
```rust
// mod alloc::vec::spec_extend;
pub(super) trait SpecExtend<T, I> {
fn spec_extend(&mut self, iter: I);
}
impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
where
I: Iterator<Item = T>,
{ /* ... */ }
impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
where
I: TrustedLen<Item = T>,
{ /* ... */ }
impl<T, A1: Allocator, A2: Allocator> SpecExtend<T, IntoIter<T, A2>> for Vec<T, A1> { /* ... */ }
impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for Vec<T, A>
where
I: Iterator<Item = &'a T>,
T: Clone,
{ /* ... */ }
impl<'a, T: 'a + TrivialClone, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A> { /* ... */ }
</p>
</details>
<details><summary><code>alloc::vec::spec_from_elem::SpecFromElem</code></summary>
<p>
```rust
// mod alloc::vec::spec_from_elem;
pub(super) trait SpecFromElem: Sized {
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>;
}
// default impl
impl<T: Clone> SpecFromElem for T { /* ... */ }
// specializations
impl<T: Clone + IsZero> SpecFromElem for T { /* ... */ }
impl SpecFromElem for i8 { /* ... */ }
impl SpecFromElem for u8 { /* ... */ }
impl SpecFromElem for () { /* ... */ }
deltragon
reviewed
Nov 3, 2026
- refactors `alloc::ffi::c_str::CStringt::new::SpecNewImpl`.
- refactors `alloc::collections::vec_deque::SpecExtendFromWithin`.
- refactors `alloc::collections::SpecExtend`.
- refactors `alloc::collections::vec_deque::spec_from_iter::SpecFromIter`.
frank-king
force-pushed
the
feat/spec-to-downcast
branch
from
November 4, 2026 08:30
042719b to
e9c79f8
Compare
Collaborator
|
The job Click to see the possible cause of the failure (guessed by this bot) |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tracking Issue: #149242
MCP: rust-lang/compiler-team#904
TODO:
alloc::collections::vec_deque::SpecExtendFromWithinalloc::collections::SpecExtendalloc::ffi::c_str::CStringt::new::SpecNewImplalloc::vec::spec_from_elem::SpecFromElem