feat(slice-4): v0.0.49 — core2 yanked unblock + sc-service WIP (crates.io API gap found)
Slice 4 Ship 2 parcial. RESUELVE el blocker real que difirió sc-service 3
sesiones (core2 yanked), pero DESCUBRE un segundo bloqueo arquitectural que
requiere decisión de David. Build verde, 213 tests, sin regresiones.
=== RESUELTO: core2 yanked (el muro de 3 sesiones) ===
sc-network-types 0.16 → cid ^0.9 (0.9.0 yanked) → core2 ^0.4 (0.4.0 yanked,
las no-yanked son 0.3.x que no satisfacen ^0.4). Cargo no auto-selecciona
versiones yanked → resolución fallaba. Era exactamente lo documentado en
chain_spec.rs Slice 3 que difirió sc-service.
Fix: vendorizado el .crate publicado de core2 0.4.0 (yanked pero descargable)
en vendor/core2-0.4.0/ + [patch.crates-io] core2 = { path = ... }. Una path
source es seleccionable independiente del estado yanked. El git source
(technocreatives/core2 master) resultó ser workspace ≤0.3.x, no servía.
=== DESCUBIERTO: crates.io consensus crates ≠ git tag API ===
Las crates sc-/sp- consensus PUBLICADAS (sc-consensus-aura 0.52.0 +
sp-consensus-aura 0.45.0) tienen API DISTINTA al git tag polkadot-stable2509-3
bajo el MISMO número de versión:
- git tag: sc_consensus_aura::standalone::slot_duration_at +
sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration
- crates.io: slot_duration_at en root + NO existe slot InherentDataProvider
(Slot no implementa el trait; Slot::from_timestamp toma 1 arg)
polkadot-sdk reusa números de versión con cambios git no publicados.
Construir un node service real requiere migrar TODO el stack sc-/sp- consensus
a git sources del tag — la misma cirugía duplicate-crate que golpeó pallet-revive
(Ship 2 v0.0.39). Decisión arquitectural para David.
Cambios:
- Cargo.toml: [patch.crates-io] core2 path vendor + sp-timestamp 39 + sp-blockchain 41
- vendor/core2-0.4.0/: crate vendorizado (yanked artifact original)
- node Cargo.toml: 13 sc-* + sp- consensus deps + jsonrpsee/rpc + core2 unblock dep
- node src/service.rs + rpc.rs: WIP escritos (verbatim stable2509-3 + 2 fixes API
crates.io), NO mod-declarados (preservados para siguiente fase)
- node src/main.rs: finding documentado, mods service/rpc neutralizados
Verificación:
- cargo check -p origin-chain-node: EXIT 0 (deps resuelven vía core2, mods off)
- cargo test --workspace: 213 verde, 0 fail, sin regresiones
- core2 0.4.0 + cid 0.9.0 ahora en Cargo.lock (vía path patch)
Próximo: decisión David — (A) migrar sc-/sp- consensus a git sources tag
stable2509-3 (real node, riesgo duplicate-crate) vs (B) mantener publicadas
(node service diferido a futura generación Substrate). Ver SLICE_4 plan §10.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
05b6c70fca
commit
e0e0b907b7
24 files changed
+12242
-227
No files matched your search
Generated
+6877
-220
File diff suppressed because it is too large.
Load diff
+26
@@ -60,6 +60,9 @@ sp-session = { version = "41.0.0", default-features = false }
|
||||
sp-genesis-builder = { version = "0.20.0", default-features = false }
|
||||
sp-consensus-aura = { version = "0.45.0", default-features = false, features = ["serde"] }
|
||||
sp-consensus-grandpa = { version = "26.0.0", default-features = false, features = ["serde"] }
|
||||
# Slice 4 v0.0.49 — node sc-service deps (stable2509-3)
|
||||
sp-timestamp = { version = "39.0.0", default-features = false }
|
||||
sp-blockchain = { version = "41.0.0" }
|
||||
|
||||
# FRAME canonical — runtime composition framework (stable2509-3)
|
||||
frame-system = { version = "43.0.1", default-features = false }
|
||||
@@ -213,6 +216,29 @@ origin-chain-node = { path = "crates/origin-chain-node", version = "0.0.1" }
|
||||
getrandom_02 = { package = "getrandom", version = "0.2", default-features = false, features = ["custom"] }
|
||||
getrandom_03 = { package = "getrandom", version = "0.3", default-features = false, features = ["wasm_js"] }
|
||||
|
||||
# ============================================================
|
||||
# Slice 4 v0.0.49 — [patch.crates-io] yanked dep unblock
|
||||
# ============================================================
|
||||
#
|
||||
# `sc-network-types 0.16.0` (stable2509-3) depende de `cid ^0.9`. La única
|
||||
# versión 0.9.x es `cid 0.9.0` (yanked), que a su vez requiere `core2 ^0.4` —
|
||||
# y `core2 0.4.0` también está yanked (las publicadas no-yanked son 0.3.x, que
|
||||
# NO satisfacen `^0.4`). Cargo no auto-selecciona versiones yanked, así que la
|
||||
# resolución falla. polkadot-sdk upstream lo evita porque commitea su Cargo.lock
|
||||
# con esas versiones; al consumir los crates publicados sin ese lock, golpeamos
|
||||
# el wart.
|
||||
#
|
||||
# Fix: vendorizamos el `.crate` publicado de `core2 0.4.0` (yanked pero aún
|
||||
# descargable desde static.crates.io) en `vendor/core2-0.4.0/` y lo aplicamos
|
||||
# como patch path. Una path source SÍ es seleccionable por cargo independiente
|
||||
# del estado yanked, y su Cargo.toml declara exactamente 0.4.0 → satisface el
|
||||
# `^0.4` de cid sin tocar el árbol sc-*. (El git source master resultó ser un
|
||||
# workspace con versión ≤0.3.x, no servía.) Era el blocker real que difirió
|
||||
# sc-service en Slice 3 (documentado en chain_spec.rs: "cid 0.9.0 yanked core2
|
||||
# 0.4.0"). El crate vendorizado es idéntico al artefacto publicado original.
|
||||
[patch.crates-io]
|
||||
core2 = { path = "vendor/core2-0.4.0" }
|
||||
|
||||
[profile.release]
|
||||
# Slice 2b finding canonical: lto/codegen-units customization triggers alloc
|
||||
# recompile en wbuild → duplicate exchange_malloc lang item canonical. Defaults
|
||||
|
||||
@@ -7,19 +7,69 @@ license.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
homepage.workspace = true
|
||||
description = "Origin Chain node binary canonical. Phase 1.6+ Slice 1 (v0.0.10): CLI skeleton + preset listing. Slice 3 (v0.0.13): real sc_chain_spec::GenericChainSpec builder canonical via mock-WASM fallback. Real sc-service builder + JSON-RPC + WASM binary canonical land Slices 2c + 4."
|
||||
description = "Origin Chain node binary. Phase 1.6+ Slice 4 (v0.0.49+): full sc-service builder (Aura + Grandpa consensus + JSON-RPC + libp2p networking + telemetry) + sc_service::GenericChainSpec con WASM_BINARY real. Pilar 4 settlement layer."
|
||||
|
||||
[[bin]]
|
||||
name = "origin-chain-node"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
origin-chain-runtime.workspace = true
|
||||
sp-genesis-builder.workspace = true
|
||||
# Slice 3 canonical (v0.0.13) — own minimal ChainSpec JSON builder canonical via
|
||||
# serde_json puro. sc-chain-spec dep canonical evitada (43/44.0.0 trae sc-network
|
||||
# 0.50/0.51 mismatch + cid 0.9.0 yanked core2). Real sc_chain_spec::GenericChainSpec
|
||||
# canonical land en Slice 4 post workspace sc-* upgrade canonical sin yanked deps.
|
||||
# Runtime (std host-side). El feature `wasm-build` (opt-in, ver [features]) se
|
||||
# propaga al runtime para poblar WASM_BINARY consumido por chain_spec.
|
||||
origin-chain-runtime = { workspace = true, features = ["std"] }
|
||||
|
||||
# ── Substrate client (sc-service stack, stable2509-3) ──────────
|
||||
sc-cli.workspace = true
|
||||
sc-service.workspace = true
|
||||
sc-executor.workspace = true
|
||||
sc-network.workspace = true
|
||||
sc-consensus.workspace = true
|
||||
sc-consensus-aura.workspace = true
|
||||
sc-consensus-grandpa.workspace = true
|
||||
sc-transaction-pool.workspace = true
|
||||
sc-transaction-pool-api.workspace = true
|
||||
sc-client-api.workspace = true
|
||||
sc-basic-authorship.workspace = true
|
||||
sc-telemetry.workspace = true
|
||||
sc-offchain.workspace = true
|
||||
|
||||
# ── Substrate primitives (host-side, std) ─────────────────────
|
||||
sp-core = { workspace = true, features = ["std"] }
|
||||
sp-runtime = { workspace = true, features = ["std"] }
|
||||
sp-io = { workspace = true, features = ["std"] }
|
||||
sp-timestamp = { workspace = true, features = ["std"] }
|
||||
sp-consensus-aura = { workspace = true, features = ["std"] }
|
||||
sp-api = { workspace = true, features = ["std"] }
|
||||
sp-block-builder = { workspace = true, features = ["std"] }
|
||||
sp-genesis-builder = { workspace = true, features = ["std"] }
|
||||
sp-blockchain.workspace = true
|
||||
|
||||
# ── RPC server (jsonrpsee) ────────────────────────────────────
|
||||
jsonrpsee.workspace = true
|
||||
substrate-frame-rpc-system.workspace = true
|
||||
pallet-transaction-payment-rpc.workspace = true
|
||||
|
||||
# ── CLI + async + utils ───────────────────────────────────────
|
||||
clap = { workspace = true }
|
||||
futures.workspace = true
|
||||
serde_json.workspace = true
|
||||
log.workspace = true
|
||||
|
||||
# ── Yanked-version unblock (Slice 4 stable2509-3 ecosystem wart) ──
|
||||
# `sc-network-types 0.16.0` depende de `cid ^0.9`, y `cid 0.9.0` (única 0.9.x,
|
||||
# yanked) requiere `core2 ^0.4` — y `core2 0.4.0` también está yanked (las
|
||||
# no-yanked son 0.3.x, que NO satisfacen ^0.4). La resolución falla porque cargo
|
||||
# no auto-selecciona versiones yanked. El pin exacto `=` SÍ permite versiones
|
||||
# yanked, forzando 0.4.0 al grafo para satisfacer la dependencia transitiva de
|
||||
# cid. Dep no usada en código — solo desbloquea el resolver. Era el blocker real
|
||||
# que difirió sc-service en Slice 3 (documentado en chain_spec.rs).
|
||||
core2 = { version = "=0.4.0", default-features = false }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
# Slice 4: activa la generación del WASM_BINARY real (runtime build vía
|
||||
# substrate-wasm-builder). Requiere env vars CC=LLVM (ver SLICE_2E). Sin este
|
||||
# feature el node compila host-only pero `--dev` falla en runtime con mensaje
|
||||
# claro (WASM_BINARY=None). Mantiene `cargo test --workspace` rápido sin env vars.
|
||||
wasm-build = ["origin-chain-runtime/wasm-build"]
|
||||
runtime-benchmarks = ["origin-chain-runtime/runtime-benchmarks"]
|
||||
@@ -35,6 +35,26 @@
|
||||
//! posibles canonical están a la vista canonical, BM V2 incluida canonical.
|
||||
|
||||
mod chain_spec;
|
||||
// Slice 4 Paso 4c (v0.0.49): módulos sc-service `service.rs` + `rpc.rs` escritos
|
||||
// pero NO compilados aún — ver bloqueo arquitectural documentado abajo.
|
||||
//
|
||||
// BLOQUEO (hallazgo v0.0.49): las crates sc-*/sp- consensus PUBLICADAS en
|
||||
// crates.io (sc-consensus-aura 0.52.0 + sp-consensus-aura 0.45.0) tienen una API
|
||||
// DISTINTA al git tag polkadot-stable2509-3 bajo el MISMO número de versión.
|
||||
// El tag usa `sc_consensus_aura::standalone::slot_duration_at` +
|
||||
// `sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration`;
|
||||
// las publicadas tienen `slot_duration_at` en root y NO tienen un slot
|
||||
// `InherentDataProvider` (Slot no implementa el trait). polkadot-sdk reusa
|
||||
// números de versión con cambios git no publicados.
|
||||
//
|
||||
// Construir un node service real requiere migrar TODO el stack sc-*/sp- consensus
|
||||
// a git sources del tag stable2509-3 — la misma cirugía de duplicate-crate que
|
||||
// golpeó pallet-revive (Ship 2 v0.0.39). Es una decisión arquitectural para David.
|
||||
// Ver docs/SLICE_4_SC_SERVICE_IMPLEMENTATION_PLAN_V1.md §10 (finding) para opciones.
|
||||
//
|
||||
// `service.rs`/`rpc.rs` se conservan como archivos (no `mod`-declarados) para la
|
||||
// siguiente fase. El core2 yanked unblock (Cargo.toml + vendor/) SÍ quedó resuelto
|
||||
// — era el blocker real de resolución que difirió sc-service 3 sesiones.
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use origin_chain_runtime::{
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
//! Origin Chain node RPC (Slice 4 Paso 4c).
|
||||
//!
|
||||
//! Patrón estándar solochain-template `polkadot-stable2509-3`. Expone System
|
||||
//! (account nonce) + TransactionPayment (fee queries) sobre el client del node.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use jsonrpsee::RpcModule;
|
||||
use origin_chain_runtime::{opaque::Block, AccountId, Balance, Nonce};
|
||||
use sc_transaction_pool_api::TransactionPool;
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_block_builder::BlockBuilder;
|
||||
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
|
||||
|
||||
/// Dependencias completas para construir los RPC del node.
|
||||
pub struct FullDeps<C, P> {
|
||||
/// El client del node.
|
||||
pub client: Arc<C>,
|
||||
/// El transaction pool del node.
|
||||
pub pool: Arc<P>,
|
||||
}
|
||||
|
||||
/// Instancia los RPC handlers completos (System + TransactionPayment).
|
||||
pub fn create_full<C, P>(
|
||||
deps: FullDeps<C, P>,
|
||||
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
|
||||
where
|
||||
C: ProvideRuntimeApi<Block>
|
||||
+ HeaderBackend<Block>
|
||||
+ HeaderMetadata<Block, Error = BlockChainError>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>
|
||||
+ pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>
|
||||
+ BlockBuilder<Block>,
|
||||
P: TransactionPool + 'static,
|
||||
{
|
||||
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
|
||||
use substrate_frame_rpc_system::{System, SystemApiServer};
|
||||
|
||||
let mut module = RpcModule::new(());
|
||||
let FullDeps { client, pool } = deps;
|
||||
|
||||
module.merge(System::new(client.clone(), pool).into_rpc())?;
|
||||
module.merge(TransactionPayment::new(client).into_rpc())?;
|
||||
|
||||
Ok(module)
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
//! Origin Chain node service (Slice 4 Paso 4c).
|
||||
//!
|
||||
//! Verbatim del solochain-template `polkadot-stable2509-3`
|
||||
//! (`templates/solochain/node/src/service.rs`), con un único cambio: el import
|
||||
//! del runtime apunta a `origin_chain_runtime`. Aura (block production) + Grandpa
|
||||
//! (finality) + JSON-RPC + libp2p networking + telemetry.
|
||||
|
||||
use futures::FutureExt;
|
||||
use sc_client_api::{Backend, BlockBackend};
|
||||
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
|
||||
use sc_consensus_grandpa::SharedVoterState;
|
||||
use sc_service::{error::Error as ServiceError, Configuration, TaskManager, WarpSyncConfig};
|
||||
use sc_telemetry::{Telemetry, TelemetryWorker};
|
||||
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
|
||||
use origin_chain_runtime::{opaque::Block, RuntimeApi};
|
||||
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
pub(crate) type FullClient = sc_service::TFullClient<
|
||||
Block,
|
||||
RuntimeApi,
|
||||
sc_executor::WasmExecutor<sp_io::SubstrateHostFunctions>,
|
||||
>;
|
||||
type FullBackend = sc_service::TFullBackend<Block>;
|
||||
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
|
||||
|
||||
/// Periodo mínimo de bloques sobre los que se importan/generan justifications.
|
||||
const GRANDPA_JUSTIFICATION_PERIOD: u32 = 512;
|
||||
|
||||
pub type Service = sc_service::PartialComponents<
|
||||
FullClient,
|
||||
FullBackend,
|
||||
FullSelectChain,
|
||||
sc_consensus::DefaultImportQueue<Block>,
|
||||
sc_transaction_pool::TransactionPoolHandle<Block, FullClient>,
|
||||
(
|
||||
sc_consensus_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,
|
||||
sc_consensus_grandpa::LinkHalf<Block, FullClient, FullSelectChain>,
|
||||
Option<Telemetry>,
|
||||
),
|
||||
>;
|
||||
|
||||
pub fn new_partial(config: &Configuration) -> Result<Service, ServiceError> {
|
||||
let telemetry = config
|
||||
.telemetry_endpoints
|
||||
.clone()
|
||||
.filter(|x| !x.is_empty())
|
||||
.map(|endpoints| -> Result<_, sc_telemetry::Error> {
|
||||
let worker = TelemetryWorker::new(16)?;
|
||||
let telemetry = worker.handle().new_telemetry(endpoints);
|
||||
Ok((worker, telemetry))
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let executor = sc_service::new_wasm_executor::<sp_io::SubstrateHostFunctions>(&config.executor);
|
||||
let (client, backend, keystore_container, task_manager) =
|
||||
sc_service::new_full_parts::<Block, RuntimeApi, _>(
|
||||
config,
|
||||
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
|
||||
executor,
|
||||
)?;
|
||||
let client = Arc::new(client);
|
||||
|
||||
let telemetry = telemetry.map(|(worker, telemetry)| {
|
||||
task_manager.spawn_handle().spawn("telemetry", None, worker.run());
|
||||
telemetry
|
||||
});
|
||||
|
||||
let select_chain = sc_consensus::LongestChain::new(backend.clone());
|
||||
|
||||
let transaction_pool = Arc::from(
|
||||
sc_transaction_pool::Builder::new(
|
||||
task_manager.spawn_essential_handle(),
|
||||
client.clone(),
|
||||
config.role.is_authority().into(),
|
||||
)
|
||||
.with_options(config.transaction_pool.clone())
|
||||
.with_prometheus(config.prometheus_registry())
|
||||
.build(),
|
||||
);
|
||||
|
||||
let (grandpa_block_import, grandpa_link) = sc_consensus_grandpa::block_import(
|
||||
client.clone(),
|
||||
GRANDPA_JUSTIFICATION_PERIOD,
|
||||
&client,
|
||||
select_chain.clone(),
|
||||
telemetry.as_ref().map(|x| x.handle()),
|
||||
)?;
|
||||
|
||||
let cidp_client = client.clone();
|
||||
let import_queue =
|
||||
sc_consensus_aura::import_queue::<AuraPair, _, _, _, _, _>(ImportQueueParams {
|
||||
block_import: grandpa_block_import.clone(),
|
||||
justification_import: Some(Box::new(grandpa_block_import.clone())),
|
||||
client: client.clone(),
|
||||
create_inherent_data_providers: move |parent_hash, _| {
|
||||
let cidp_client = cidp_client.clone();
|
||||
async move {
|
||||
// sc-consensus-aura 0.52: slot_duration_at vive en el root del
|
||||
// crate (no en `standalone::`, que es de versiones posteriores).
|
||||
let slot_duration = sc_consensus_aura::slot_duration_at(
|
||||
&*cidp_client,
|
||||
parent_hash,
|
||||
)?;
|
||||
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
|
||||
|
||||
// crates.io sp-consensus-aura 0.45: el slot se deriva del
|
||||
// timestamp vía `Slot::from_timestamp` (no existe
|
||||
// `inherents::InherentDataProvider::from_timestamp_and_slot_duration`,
|
||||
// que es de un snapshot git posterior con la misma versión).
|
||||
let slot =
|
||||
sp_consensus_aura::Slot::from_timestamp(*timestamp, slot_duration);
|
||||
|
||||
Ok((slot, timestamp))
|
||||
}
|
||||
},
|
||||
spawner: &task_manager.spawn_essential_handle(),
|
||||
registry: config.prometheus_registry(),
|
||||
check_for_equivocation: Default::default(),
|
||||
telemetry: telemetry.as_ref().map(|x| x.handle()),
|
||||
compatibility_mode: Default::default(),
|
||||
})?;
|
||||
|
||||
Ok(sc_service::PartialComponents {
|
||||
client,
|
||||
backend,
|
||||
task_manager,
|
||||
import_queue,
|
||||
keystore_container,
|
||||
select_chain,
|
||||
transaction_pool,
|
||||
other: (grandpa_block_import, grandpa_link, telemetry),
|
||||
})
|
||||
}
|
||||
|
||||
/// Construye un nuevo service para un full client.
|
||||
pub fn new_full<
|
||||
N: sc_network::NetworkBackend<Block, <Block as sp_runtime::traits::Block>::Hash>,
|
||||
>(
|
||||
config: Configuration,
|
||||
) -> Result<TaskManager, ServiceError> {
|
||||
let sc_service::PartialComponents {
|
||||
client,
|
||||
backend,
|
||||
mut task_manager,
|
||||
import_queue,
|
||||
keystore_container,
|
||||
select_chain,
|
||||
transaction_pool,
|
||||
other: (block_import, grandpa_link, mut telemetry),
|
||||
} = new_partial(&config)?;
|
||||
|
||||
let mut net_config = sc_network::config::FullNetworkConfiguration::<
|
||||
Block,
|
||||
<Block as sp_runtime::traits::Block>::Hash,
|
||||
N,
|
||||
>::new(&config.network, config.prometheus_registry().cloned());
|
||||
let metrics = N::register_notification_metrics(config.prometheus_registry());
|
||||
|
||||
let peer_store_handle = net_config.peer_store_handle();
|
||||
let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name(
|
||||
&client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"),
|
||||
&config.chain_spec,
|
||||
);
|
||||
let (grandpa_protocol_config, grandpa_notification_service) =
|
||||
sc_consensus_grandpa::grandpa_peers_set_config::<_, N>(
|
||||
grandpa_protocol_name.clone(),
|
||||
metrics.clone(),
|
||||
peer_store_handle,
|
||||
);
|
||||
net_config.add_notification_protocol(grandpa_protocol_config);
|
||||
|
||||
let warp_sync = Arc::new(sc_consensus_grandpa::warp_proof::NetworkProvider::new(
|
||||
backend.clone(),
|
||||
grandpa_link.shared_authority_set().clone(),
|
||||
Vec::default(),
|
||||
));
|
||||
|
||||
let (network, system_rpc_tx, tx_handler_controller, sync_service) =
|
||||
sc_service::build_network(sc_service::BuildNetworkParams {
|
||||
config: &config,
|
||||
net_config,
|
||||
client: client.clone(),
|
||||
transaction_pool: transaction_pool.clone(),
|
||||
spawn_handle: task_manager.spawn_handle(),
|
||||
import_queue,
|
||||
block_announce_validator_builder: None,
|
||||
warp_sync_config: Some(WarpSyncConfig::WithProvider(warp_sync)),
|
||||
block_relay: None,
|
||||
metrics,
|
||||
})?;
|
||||
|
||||
if config.offchain_worker.enabled {
|
||||
let offchain_workers =
|
||||
sc_offchain::OffchainWorkers::new(sc_offchain::OffchainWorkerOptions {
|
||||
runtime_api_provider: client.clone(),
|
||||
is_validator: config.role.is_authority(),
|
||||
keystore: Some(keystore_container.keystore()),
|
||||
offchain_db: backend.offchain_storage(),
|
||||
transaction_pool: Some(OffchainTransactionPoolFactory::new(
|
||||
transaction_pool.clone(),
|
||||
)),
|
||||
network_provider: Arc::new(network.clone()),
|
||||
enable_http_requests: true,
|
||||
custom_extensions: |_| vec![],
|
||||
})?;
|
||||
task_manager.spawn_handle().spawn(
|
||||
"offchain-workers-runner",
|
||||
"offchain-worker",
|
||||
offchain_workers.run(client.clone(), task_manager.spawn_handle()).boxed(),
|
||||
);
|
||||
}
|
||||
|
||||
let role = config.role;
|
||||
let force_authoring = config.force_authoring;
|
||||
let backoff_authoring_blocks: Option<()> = None;
|
||||
let name = config.network.node_name.clone();
|
||||
let enable_grandpa = !config.disable_grandpa;
|
||||
let prometheus_registry = config.prometheus_registry().cloned();
|
||||
|
||||
let rpc_extensions_builder = {
|
||||
let client = client.clone();
|
||||
let pool = transaction_pool.clone();
|
||||
|
||||
Box::new(move |_| {
|
||||
let deps = crate::rpc::FullDeps { client: client.clone(), pool: pool.clone() };
|
||||
crate::rpc::create_full(deps).map_err(Into::into)
|
||||
})
|
||||
};
|
||||
|
||||
let _rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams {
|
||||
network: Arc::new(network.clone()),
|
||||
client: client.clone(),
|
||||
keystore: keystore_container.keystore(),
|
||||
task_manager: &mut task_manager,
|
||||
transaction_pool: transaction_pool.clone(),
|
||||
rpc_builder: rpc_extensions_builder,
|
||||
backend,
|
||||
system_rpc_tx,
|
||||
tx_handler_controller,
|
||||
sync_service: sync_service.clone(),
|
||||
config,
|
||||
telemetry: telemetry.as_mut(),
|
||||
})?;
|
||||
|
||||
if role.is_authority() {
|
||||
let proposer_factory = sc_basic_authorship::ProposerFactory::new(
|
||||
task_manager.spawn_handle(),
|
||||
client.clone(),
|
||||
transaction_pool.clone(),
|
||||
prometheus_registry.as_ref(),
|
||||
telemetry.as_ref().map(|x| x.handle()),
|
||||
);
|
||||
|
||||
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
|
||||
|
||||
let aura = sc_consensus_aura::start_aura::<AuraPair, _, _, _, _, _, _, _, _, _, _>(
|
||||
StartAuraParams {
|
||||
slot_duration,
|
||||
client,
|
||||
select_chain,
|
||||
block_import,
|
||||
proposer_factory,
|
||||
create_inherent_data_providers: move |_, ()| async move {
|
||||
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
|
||||
|
||||
// Mismo patrón que el import_queue CIDP (crates.io 0.45 API).
|
||||
let slot =
|
||||
sp_consensus_aura::Slot::from_timestamp(*timestamp, slot_duration);
|
||||
|
||||
Ok((slot, timestamp))
|
||||
},
|
||||
force_authoring,
|
||||
backoff_authoring_blocks,
|
||||
keystore: keystore_container.keystore(),
|
||||
sync_oracle: sync_service.clone(),
|
||||
justification_sync_link: sync_service.clone(),
|
||||
block_proposal_slot_portion: SlotProportion::new(2f32 / 3f32),
|
||||
max_block_proposal_slot_portion: None,
|
||||
telemetry: telemetry.as_ref().map(|x| x.handle()),
|
||||
compatibility_mode: Default::default(),
|
||||
},
|
||||
)?;
|
||||
|
||||
// La tarea de authoring de AURA es esencial: si falla, baja el service.
|
||||
task_manager
|
||||
.spawn_essential_handle()
|
||||
.spawn_blocking("aura", Some("block-authoring"), aura);
|
||||
}
|
||||
|
||||
if enable_grandpa {
|
||||
// Si el node no participa activamente en consensus, no necesita keystore.
|
||||
let keystore = if role.is_authority() { Some(keystore_container.keystore()) } else { None };
|
||||
|
||||
let grandpa_config = sc_consensus_grandpa::Config {
|
||||
gossip_duration: Duration::from_millis(333),
|
||||
justification_generation_period: GRANDPA_JUSTIFICATION_PERIOD,
|
||||
name: Some(name),
|
||||
observer_enabled: false,
|
||||
keystore,
|
||||
local_role: role,
|
||||
telemetry: telemetry.as_ref().map(|x| x.handle()),
|
||||
protocol_name: grandpa_protocol_name,
|
||||
};
|
||||
|
||||
let grandpa_config = sc_consensus_grandpa::GrandpaParams {
|
||||
config: grandpa_config,
|
||||
link: grandpa_link,
|
||||
network,
|
||||
sync: Arc::new(sync_service),
|
||||
notification_service: grandpa_notification_service,
|
||||
voting_rule: sc_consensus_grandpa::VotingRulesBuilder::default().build(),
|
||||
prometheus_registry,
|
||||
shared_voter_state: SharedVoterState::empty(),
|
||||
telemetry: telemetry.as_ref().map(|x| x.handle()),
|
||||
offchain_tx_pool_factory: OffchainTransactionPoolFactory::new(transaction_pool),
|
||||
};
|
||||
|
||||
// La tarea del voter de GRANDPA es infalible: si falla, baja el service.
|
||||
task_manager.spawn_essential_handle().spawn_blocking(
|
||||
"grandpa-voter",
|
||||
None,
|
||||
sc_consensus_grandpa::run_grandpa_voter(grandpa_config)?,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(task_manager)
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "545e84bcb0f235b12e21351e0c69767958efe2a7"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
name: CI
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test-nightly:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
rust: [nightly]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
override: true
|
||||
- name: Run tests (no_std)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --tests --no-default-features --features nightly
|
||||
- name: Run tests (std)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --tests --no-default-features --features std,nightly
|
||||
- name: Run tests (alloc)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --tests --no-default-features --features alloc,nightly
|
||||
test-stable:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
rust: [stable]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
override: true
|
||||
- name: Run tests (no_std)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --no-default-features
|
||||
- name: Run tests (std)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --no-default-features --features std
|
||||
- name: Run tests (alloc)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --no-default-features --features alloc
|
||||
test-msrv:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
rust: [1.47]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
override: true
|
||||
- name: Run tests (no_std)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --no-default-features
|
||||
- name: Run tests (std)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --no-default-features --features std
|
||||
- name: Run tests (alloc)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --no-default-features --features alloc
|
||||
@@ -0,0 +1,9 @@
|
||||
/target
|
||||
|
||||
|
||||
#Added by cargo
|
||||
#
|
||||
#already existing elements were commented out
|
||||
|
||||
#/target
|
||||
Cargo.lock
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2018"
|
||||
name = "core2"
|
||||
version = "0.4.0"
|
||||
authors = ["Brendan Molloy <brendan@bbqsrc.net>"]
|
||||
description = "The bare essentials of std::io for use in no_std. Alloc support is optional."
|
||||
categories = ["no-std"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
repository = "https://github.com/bbqsrc/core2"
|
||||
[package.metadata.docs.rs]
|
||||
features = ["nightly"]
|
||||
[dependencies.memchr]
|
||||
version = "2"
|
||||
default-features = false
|
||||
|
||||
[features]
|
||||
alloc = []
|
||||
default = ["std"]
|
||||
nightly = []
|
||||
std = ["alloc"]
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "core2"
|
||||
version = "0.4.0"
|
||||
authors = ["Brendan Molloy <brendan@bbqsrc.net>"]
|
||||
description = "The bare essentials of std::io for use in no_std. Alloc support is optional."
|
||||
license = "Apache-2.0 OR MIT"
|
||||
edition = "2018"
|
||||
repository = "https://github.com/bbqsrc/core2"
|
||||
categories = ["no-std"]
|
||||
|
||||
[dependencies]
|
||||
memchr = { version = "2", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["alloc"]
|
||||
alloc = []
|
||||
nightly = []
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["nightly"]
|
||||
Vendored
+202
@@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2020-2021 Brendan Molloy <brendan@bbqsrc.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
# core2
|
||||
|
||||
[](https://github.com/bbqsrc/core2/actions)
|
||||
[](https://docs.rs/core2)
|
||||

|
||||
|
||||
Ever wanted a `Cursor` or the `Error` trait in `no_std`? Well now you can have it. A 'fork' of Rust's `std` modules for `no_std` environments, with the added benefit of optionally taking advantage of `alloc`.
|
||||
|
||||
The goal of this crate is to provide a stable interface for building I/O and error trait functionality in
|
||||
`no_std` environments. The current code corresponds to the most recent stable API of Rust 1.47.0.
|
||||
It is also a goal to achieve a true alloc-less experience, with opt-in alloc support.
|
||||
|
||||
This crate works on `stable` with some limitations in functionality, and `nightly` without limitations by adding
|
||||
the relevant feature flag.
|
||||
|
||||
This crate is `std` by default -- use no default features to get `no_std` mode.
|
||||
|
||||
## Usage
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
core2 = "0.3"
|
||||
```
|
||||
|
||||
Add the crate, use the things you would usually want from `std::io`, but instead from `core2::io`, and
|
||||
use `core2::error::Error` in place of `std::error::Error`.
|
||||
|
||||
### Features
|
||||
|
||||
- **std**: enables `std` pass-throughs for the polyfilled types, but allows accessing the new types
|
||||
- **alloc**: enable aspects of the `Read` and `Write` traits that require `alloc` support (WIP)
|
||||
- **nightly**: enables **nightly**-only features, such as `BufReader` and `BufWriter` with const generic buffers.
|
||||
|
||||
### Differences to `std::io`
|
||||
|
||||
- No `std::io::Error`, so we have our own copy without any `Os` error functions
|
||||
- `IoSlice` and the `*_vectored` family of functions are not implemented.
|
||||
- `BufReader` and `BufWriter` have a different signature, as they now use a const generic bounded array for the internal buffer. (Requires **nightly** feature)
|
||||
|
||||
Other than items perhaps being entirely missing or certain functions unavailable on some traits, no function signatures have been changed.
|
||||
|
||||
### Limitations
|
||||
|
||||
- Using the buffer types currently requires **nightly** due to the use of const generics.
|
||||
- Using `copy` or the buffer types with `std` support currently requires **nightly** due to the `initializer` API.
|
||||
|
||||
## Where is it used?
|
||||
|
||||
All of the below are works in progress, but should help with demonstrating how to use this crate.
|
||||
|
||||
- [thiserror_core2](https://github.com/bbqsrc/thiserror-core2): fork of `thiserror` using the `core2::error::Error` trait.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
||||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
|
||||
---
|
||||
|
||||
Almost all of the code in this repository is a copy of the [Rust language codebase](https://github.com/rust-lang/rust) with minor modifications.
|
||||
|
||||
For attributions, see https://thanks.rust-lang.org/.
|
||||
Vendored
+473
@@ -0,0 +1,473 @@
|
||||
//! Traits for working with Errors.
|
||||
|
||||
// A note about crates and the facade:
|
||||
//
|
||||
// Originally, the `Error` trait was defined in libcore, and the impls
|
||||
// were scattered about. However, coherence objected to this
|
||||
// arrangement, because to create the blanket impls for `Box` required
|
||||
// knowing that `&str: !Error`, and we have no means to deal with that
|
||||
// sort of conflict just now. Therefore, for the time being, we have
|
||||
// moved the `Error` trait into libstd. As we evolve a sol'n to the
|
||||
// coherence challenge (e.g., specialization, neg impls, etc) we can
|
||||
// reconsider what crate these items belong in.
|
||||
|
||||
#[allow(deprecated)]
|
||||
use core::alloc::LayoutErr;
|
||||
|
||||
use core::any::TypeId;
|
||||
use core::fmt::{Debug, Display};
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::{string::String, borrow::Cow, boxed::Box};
|
||||
|
||||
/// `Error` is a trait representing the basic expectations for error values,
|
||||
/// i.e., values of type `E` in [`Result<T, E>`]. Errors must describe
|
||||
/// themselves through the [`Display`] and [`Debug`] traits, and may provide
|
||||
/// cause chain information:
|
||||
///
|
||||
/// [`Error::source()`] is generally used when errors cross
|
||||
/// "abstraction boundaries". If one module must report an error that is caused
|
||||
/// by an error from a lower-level module, it can allow accessing that error
|
||||
/// via [`Error::source()`]. This makes it possible for the high-level
|
||||
/// module to provide its own errors while also revealing some of the
|
||||
/// implementation for debugging via `source` chains.
|
||||
///
|
||||
/// [`Result<T, E>`]: Result
|
||||
pub trait Error: Debug + Display {
|
||||
/// The lower-level source of this error, if any.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::fmt;
|
||||
///
|
||||
/// #[derive(Debug)]
|
||||
/// struct SuperError {
|
||||
/// side: SuperErrorSideKick,
|
||||
/// }
|
||||
///
|
||||
/// impl fmt::Display for SuperError {
|
||||
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
/// write!(f, "SuperError is here!")
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl Error for SuperError {
|
||||
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
/// Some(&self.side)
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug)]
|
||||
/// struct SuperErrorSideKick;
|
||||
///
|
||||
/// impl fmt::Display for SuperErrorSideKick {
|
||||
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
/// write!(f, "SuperErrorSideKick is here!")
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl Error for SuperErrorSideKick {}
|
||||
///
|
||||
/// fn get_super_error() -> Result<(), SuperError> {
|
||||
/// Err(SuperError { side: SuperErrorSideKick })
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// match get_super_error() {
|
||||
/// Err(e) => {
|
||||
/// println!("Error: {}", e);
|
||||
/// println!("Caused by: {}", e.source().unwrap());
|
||||
/// }
|
||||
/// _ => println!("No error"),
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Gets the `TypeId` of `self`.
|
||||
#[doc(hidden)]
|
||||
fn type_id(&self, _: private::Internal) -> TypeId
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
TypeId::of::<Self>()
|
||||
}
|
||||
}
|
||||
|
||||
mod private {
|
||||
// This is a hack to prevent `type_id` from being overridden by `Error`
|
||||
// implementations, since that can enable unsound downcasting.
|
||||
#[derive(Debug)]
|
||||
pub struct Internal;
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
|
||||
/// Converts a type of [`Error`] into a box of dyn [`Error`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::fmt;
|
||||
/// use std::mem;
|
||||
///
|
||||
/// #[derive(Debug)]
|
||||
/// struct AnError;
|
||||
///
|
||||
/// impl fmt::Display for AnError {
|
||||
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
/// write!(f , "An error")
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl Error for AnError {}
|
||||
///
|
||||
/// let an_error = AnError;
|
||||
/// assert!(0 == mem::size_of_val(&an_error));
|
||||
/// let a_boxed_error = Box::<dyn Error>::from(an_error);
|
||||
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
fn from(err: E) -> Box<dyn Error + 'a> {
|
||||
Box::new(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
|
||||
/// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of
|
||||
/// dyn [`Error`] + [`Send`] + [`Sync`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::fmt;
|
||||
/// use std::mem;
|
||||
///
|
||||
/// #[derive(Debug)]
|
||||
/// struct AnError;
|
||||
///
|
||||
/// impl fmt::Display for AnError {
|
||||
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
/// write!(f , "An error")
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl Error for AnError {}
|
||||
///
|
||||
/// unsafe impl Send for AnError {}
|
||||
///
|
||||
/// unsafe impl Sync for AnError {}
|
||||
///
|
||||
/// let an_error = AnError;
|
||||
/// assert!(0 == mem::size_of_val(&an_error));
|
||||
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
|
||||
/// assert!(
|
||||
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
|
||||
Box::new(err)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl From<String> for Box<dyn Error + Send + Sync> {
|
||||
/// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::mem;
|
||||
///
|
||||
/// let a_string_error = "a string error".to_string();
|
||||
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
|
||||
/// assert!(
|
||||
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from(err: String) -> Box<dyn Error + Send + Sync> {
|
||||
struct StringError(String);
|
||||
|
||||
impl Error for StringError {}
|
||||
|
||||
impl Display for StringError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
Display::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
// Purposefully skip printing "StringError(..)"
|
||||
impl Debug for StringError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
Debug::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
|
||||
Box::new(StringError(err))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl From<String> for Box<dyn Error> {
|
||||
/// Converts a [`String`] into a box of dyn [`Error`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::mem;
|
||||
///
|
||||
/// let a_string_error = "a string error".to_string();
|
||||
/// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
|
||||
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
fn from(str_err: String) -> Box<dyn Error> {
|
||||
let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
|
||||
let err2: Box<dyn Error> = err1;
|
||||
err2
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
|
||||
/// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
||||
///
|
||||
/// [`str`]: prim@str
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::mem;
|
||||
///
|
||||
/// let a_str_error = "a str error";
|
||||
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
|
||||
/// assert!(
|
||||
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
|
||||
From::from(String::from(err))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl From<&str> for Box<dyn Error> {
|
||||
/// Converts a [`str`] into a box of dyn [`Error`].
|
||||
///
|
||||
/// [`str`]: prim@str
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::mem;
|
||||
///
|
||||
/// let a_str_error = "a str error";
|
||||
/// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
|
||||
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
fn from(err: &str) -> Box<dyn Error> {
|
||||
From::from(String::from(err))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
|
||||
/// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::mem;
|
||||
/// use std::borrow::Cow;
|
||||
///
|
||||
/// let a_cow_str_error = Cow::from("a str error");
|
||||
/// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
|
||||
/// assert!(
|
||||
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
|
||||
From::from(String::from(err))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
|
||||
/// Converts a [`Cow`] into a box of dyn [`Error`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::error::Error;
|
||||
/// use std::mem;
|
||||
/// use std::borrow::Cow;
|
||||
///
|
||||
/// let a_cow_str_error = Cow::from("a str error");
|
||||
/// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
|
||||
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
|
||||
/// ```
|
||||
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
|
||||
From::from(String::from(err))
|
||||
}
|
||||
}
|
||||
|
||||
// #[unstable(feature = "never_type", issue = "35121")]
|
||||
#[cfg(feature = "nightly")]
|
||||
impl Error for ! {}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl Error for LayoutErr {}
|
||||
|
||||
impl Error for core::str::ParseBoolError {}
|
||||
|
||||
impl Error for core::str::Utf8Error {}
|
||||
|
||||
impl Error for core::num::ParseIntError {}
|
||||
|
||||
impl Error for core::num::TryFromIntError {}
|
||||
|
||||
impl Error for core::array::TryFromSliceError {}
|
||||
|
||||
impl Error for core::num::ParseFloatError {}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl Error for alloc::string::FromUtf8Error {}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl Error for alloc::string::FromUtf16Error {}
|
||||
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<T: Error> Error for Box<T> {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
Error::source(&**self)
|
||||
}
|
||||
}
|
||||
|
||||
// Copied from `any.rs`.
|
||||
impl dyn Error + 'static {
|
||||
/// Returns `true` if the boxed type is the same as `T`
|
||||
#[inline]
|
||||
pub fn is<T: Error + 'static>(&self) -> bool {
|
||||
// Get `TypeId` of the type this function is instantiated with.
|
||||
let t = TypeId::of::<T>();
|
||||
|
||||
// Get `TypeId` of the type in the trait object.
|
||||
let boxed = self.type_id(private::Internal);
|
||||
|
||||
// Compare both `TypeId`s on equality.
|
||||
t == boxed
|
||||
}
|
||||
|
||||
/// Returns some reference to the boxed value if it is of type `T`, or
|
||||
/// `None` if it isn't.
|
||||
#[inline]
|
||||
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
||||
if self.is::<T>() {
|
||||
unsafe { Some(&*(self as *const dyn Error as *const T)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns some mutable reference to the boxed value if it is of type `T`, or
|
||||
/// `None` if it isn't.
|
||||
#[inline]
|
||||
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
||||
if self.is::<T>() {
|
||||
unsafe { Some(&mut *(self as *mut dyn Error as *mut T)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Error + 'static + Send {
|
||||
/// Forwards to the method defined on the type `dyn Error`.
|
||||
#[inline]
|
||||
pub fn is<T: Error + 'static>(&self) -> bool {
|
||||
<dyn Error + 'static>::is::<T>(self)
|
||||
}
|
||||
|
||||
/// Forwards to the method defined on the type `dyn Error`.
|
||||
#[inline]
|
||||
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
||||
<dyn Error + 'static>::downcast_ref::<T>(self)
|
||||
}
|
||||
|
||||
/// Forwards to the method defined on the type `dyn Error`.
|
||||
#[inline]
|
||||
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
||||
<dyn Error + 'static>::downcast_mut::<T>(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Error + 'static + Send + Sync {
|
||||
/// Forwards to the method defined on the type `dyn Error`.
|
||||
#[inline]
|
||||
pub fn is<T: Error + 'static>(&self) -> bool {
|
||||
<dyn Error + 'static>::is::<T>(self)
|
||||
}
|
||||
|
||||
/// Forwards to the method defined on the type `dyn Error`.
|
||||
#[inline]
|
||||
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
|
||||
<dyn Error + 'static>::downcast_ref::<T>(self)
|
||||
}
|
||||
|
||||
/// Forwards to the method defined on the type `dyn Error`.
|
||||
#[inline]
|
||||
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
|
||||
<dyn Error + 'static>::downcast_mut::<T>(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl dyn Error {
|
||||
#[inline]
|
||||
/// Attempts to downcast the box to a concrete type.
|
||||
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
|
||||
if self.is::<T>() {
|
||||
unsafe {
|
||||
let raw: *mut dyn Error = Box::into_raw(self);
|
||||
Ok(Box::from_raw(raw as *mut T))
|
||||
}
|
||||
} else {
|
||||
Err(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl dyn Error + Send {
|
||||
#[inline]
|
||||
/// Attempts to downcast the box to a concrete type.
|
||||
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
|
||||
let err: Box<dyn Error> = self;
|
||||
<dyn Error>::downcast(err).map_err(|s| unsafe {
|
||||
// Reapply the `Send` marker.
|
||||
core::mem::transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl dyn Error + Send + Sync {
|
||||
#[inline]
|
||||
/// Attempts to downcast the box to a concrete type.
|
||||
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
|
||||
let err: Box<dyn Error> = self;
|
||||
<dyn Error>::downcast(err).map_err(|s| unsafe {
|
||||
// Reapply the `Send + Sync` marker.
|
||||
core::mem::transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
|
||||
})
|
||||
}
|
||||
}
|
||||
+1111
File diff suppressed because it is too large.
Load diff
Vendored
+272
@@ -0,0 +1,272 @@
|
||||
use super::{BufRead, Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};
|
||||
use core::cmp;
|
||||
|
||||
/// A `Cursor` wraps an in-memory buffer and provides it with a
|
||||
/// [`Seek`] implementation.
|
||||
///
|
||||
/// `Cursor`s are used with in-memory buffers, anything implementing
|
||||
/// [`AsRef`]`<[u8]>`, to allow them to implement [`Read`] and/or [`Write`],
|
||||
/// allowing these buffers to be used anywhere you might use a reader or writer
|
||||
/// that does actual I/O.
|
||||
///
|
||||
/// The standard library implements some I/O traits on various types which
|
||||
/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
|
||||
/// `Cursor<`[`&[u8]`][bytes]`>`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// We may want to write bytes to a [`File`] in our production
|
||||
/// code, but use an in-memory buffer in our tests. We can do this with
|
||||
/// `Cursor`:
|
||||
///
|
||||
/// [bytes]: crate::slice
|
||||
/// [`File`]: crate::fs::File
|
||||
///
|
||||
/// ```
|
||||
/// use std::io::prelude::*;
|
||||
/// use core2::io::{self, Seek, SeekFrom, Write};
|
||||
/// use std::fs::File;
|
||||
///
|
||||
/// // a library function we've written
|
||||
/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
|
||||
/// writer.seek(SeekFrom::End(-10))?;
|
||||
///
|
||||
/// for i in 0..10 {
|
||||
/// writer.write(&[i])?;
|
||||
/// }
|
||||
///
|
||||
/// // all went well
|
||||
/// Ok(())
|
||||
/// }
|
||||
///
|
||||
/// # #[cfg(feature = "std")]
|
||||
/// # fn foo() -> io::Result<()> {
|
||||
/// // Here's some code that uses this library function.
|
||||
/// //
|
||||
/// // We might want to use a BufReader here for efficiency, but let's
|
||||
/// // keep this example focused.
|
||||
/// let mut file = File::create("foo.txt").map_err(|e| io::Error::from(e))?;
|
||||
///
|
||||
/// write_ten_bytes_at_end(&mut file)?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
///
|
||||
/// // now let's write a test
|
||||
/// #[test]
|
||||
/// fn test_writes_bytes() {
|
||||
/// // setting up a real File is much slower than an in-memory buffer,
|
||||
/// // let's use a cursor instead
|
||||
/// use core2::io::Cursor;
|
||||
/// let mut buff = Cursor::new(vec![0; 15]);
|
||||
///
|
||||
/// write_ten_bytes_at_end(&mut buff).unwrap();
|
||||
///
|
||||
/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct Cursor<T> {
|
||||
inner: T,
|
||||
pos: u64,
|
||||
}
|
||||
|
||||
impl<T> Cursor<T> {
|
||||
/// Creates a new cursor wrapping the provided underlying in-memory buffer.
|
||||
///
|
||||
/// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
|
||||
/// is not empty. So writing to cursor starts with overwriting [`Vec`]
|
||||
/// content, not with appending to it.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::Cursor;
|
||||
///
|
||||
/// let buff = Cursor::new(Vec::new());
|
||||
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
||||
/// # force_inference(&buff);
|
||||
/// ```
|
||||
pub fn new(inner: T) -> Cursor<T> {
|
||||
Cursor { pos: 0, inner }
|
||||
}
|
||||
|
||||
/// Consumes this cursor, returning the underlying value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::Cursor;
|
||||
///
|
||||
/// let buff = Cursor::new(Vec::new());
|
||||
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
||||
/// # force_inference(&buff);
|
||||
///
|
||||
/// let vec = buff.into_inner();
|
||||
/// ```
|
||||
pub fn into_inner(self) -> T {
|
||||
self.inner
|
||||
}
|
||||
|
||||
/// Gets a reference to the underlying value in this cursor.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::Cursor;
|
||||
///
|
||||
/// let buff = Cursor::new(Vec::new());
|
||||
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
||||
/// # force_inference(&buff);
|
||||
///
|
||||
/// let reference = buff.get_ref();
|
||||
/// ```
|
||||
pub fn get_ref(&self) -> &T {
|
||||
&self.inner
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the underlying value in this cursor.
|
||||
///
|
||||
/// Care should be taken to avoid modifying the internal I/O state of the
|
||||
/// underlying value as it may corrupt this cursor's position.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::Cursor;
|
||||
///
|
||||
/// let mut buff = Cursor::new(Vec::new());
|
||||
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
||||
/// # force_inference(&buff);
|
||||
///
|
||||
/// let reference = buff.get_mut();
|
||||
/// ```
|
||||
pub fn get_mut(&mut self) -> &mut T {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
/// Returns the current position of this cursor.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Cursor, Seek, SeekFrom};
|
||||
/// use std::io::prelude::*;
|
||||
///
|
||||
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
|
||||
///
|
||||
/// assert_eq!(buff.position(), 0);
|
||||
///
|
||||
/// buff.seek(SeekFrom::Current(2)).unwrap();
|
||||
/// assert_eq!(buff.position(), 2);
|
||||
///
|
||||
/// buff.seek(SeekFrom::Current(-1)).unwrap();
|
||||
/// assert_eq!(buff.position(), 1);
|
||||
/// ```
|
||||
pub fn position(&self) -> u64 {
|
||||
self.pos
|
||||
}
|
||||
|
||||
/// Sets the position of this cursor.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::Cursor;
|
||||
///
|
||||
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
|
||||
///
|
||||
/// assert_eq!(buff.position(), 0);
|
||||
///
|
||||
/// buff.set_position(2);
|
||||
/// assert_eq!(buff.position(), 2);
|
||||
///
|
||||
/// buff.set_position(4);
|
||||
/// assert_eq!(buff.position(), 4);
|
||||
/// ```
|
||||
pub fn set_position(&mut self, pos: u64) {
|
||||
self.pos = pos;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Seek for Cursor<T>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
fn seek(&mut self, style: SeekFrom) -> Result<u64> {
|
||||
let (base_pos, offset) = match style {
|
||||
SeekFrom::Start(n) => {
|
||||
self.pos = n;
|
||||
return Ok(n);
|
||||
}
|
||||
SeekFrom::End(n) => (self.inner.as_ref().len() as u64, n),
|
||||
SeekFrom::Current(n) => (self.pos, n),
|
||||
};
|
||||
let new_pos = if offset >= 0 {
|
||||
base_pos.checked_add(offset as u64)
|
||||
} else {
|
||||
base_pos.checked_sub((offset.wrapping_neg()) as u64)
|
||||
};
|
||||
match new_pos {
|
||||
Some(n) => {
|
||||
self.pos = n;
|
||||
Ok(self.pos)
|
||||
}
|
||||
None => Err(Error::new(
|
||||
ErrorKind::InvalidInput,
|
||||
"invalid seek to a negative or overflowing position",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Read for Cursor<T>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let n = Read::read(&mut self.fill_buf()?, buf)?;
|
||||
self.pos += n as u64;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
|
||||
let n = buf.len();
|
||||
Read::read_exact(&mut self.fill_buf()?, buf)?;
|
||||
self.pos += n as u64;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> BufRead for Cursor<T>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
fn fill_buf(&mut self) -> Result<&[u8]> {
|
||||
let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64);
|
||||
Ok(&self.inner.as_ref()[(amt as usize)..])
|
||||
}
|
||||
fn consume(&mut self, amt: usize) {
|
||||
self.pos += amt as u64;
|
||||
}
|
||||
}
|
||||
|
||||
// Non-resizing write implementation
|
||||
#[inline]
|
||||
fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> Result<usize> {
|
||||
let pos = cmp::min(*pos_mut, slice.len() as u64);
|
||||
let amt = (&mut slice[(pos as usize)..]).write(buf)?;
|
||||
*pos_mut += amt as u64;
|
||||
Ok(amt)
|
||||
}
|
||||
|
||||
impl Write for Cursor<&mut [u8]> {
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||
slice_write(&mut self.pos, self.inner, buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Vendored
+458
@@ -0,0 +1,458 @@
|
||||
use core::{convert::From, fmt, result};
|
||||
|
||||
/// A specialized [`Result`] type for I/O operations.
|
||||
///
|
||||
/// This type is broadly used across [`std::io`] for any operation which may
|
||||
/// produce an error.
|
||||
///
|
||||
/// This typedef is generally used to avoid writing out [`io::Error`] directly and
|
||||
/// is otherwise a direct mapping to [`Result`].
|
||||
///
|
||||
/// While usual Rust style is to import types directly, aliases of [`Result`]
|
||||
/// often are not, to make it easier to distinguish between them. [`Result`] is
|
||||
/// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
|
||||
/// will generally use `io::Result` instead of shadowing the [prelude]'s import
|
||||
/// of [`std::result::Result`][`Result`].
|
||||
///
|
||||
/// [`std::io`]: crate::io
|
||||
/// [`io::Error`]: Error
|
||||
/// [`Result`]: crate::result::Result
|
||||
/// [prelude]: crate::prelude
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// A convenience function that bubbles an `io::Result` to its caller:
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io;
|
||||
///
|
||||
/// #[cfg(feature = "std")]
|
||||
/// fn get_string() -> io::Result<String> {
|
||||
/// let mut buffer = String::new();
|
||||
///
|
||||
/// std::io::stdin().read_line(&mut buffer).map_err(|e| io::Error::from(e))?;
|
||||
///
|
||||
/// Ok(buffer)
|
||||
/// }
|
||||
/// ```
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
|
||||
/// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
|
||||
/// associated traits.
|
||||
///
|
||||
/// Errors mostly originate from the underlying OS, but custom instances of
|
||||
/// `Error` can be created with crafted error messages and a particular value of
|
||||
/// [`ErrorKind`].
|
||||
///
|
||||
/// [`Read`]: crate::io::Read
|
||||
/// [`Write`]: crate::io::Write
|
||||
/// [`Seek`]: crate::io::Seek
|
||||
pub struct Error {
|
||||
repr: Repr,
|
||||
}
|
||||
|
||||
impl crate::error::Error for Error {}
|
||||
|
||||
impl fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self.repr, f)
|
||||
}
|
||||
}
|
||||
|
||||
enum Repr {
|
||||
Simple(ErrorKind),
|
||||
Custom(Custom),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Custom {
|
||||
kind: ErrorKind,
|
||||
error: &'static str,
|
||||
}
|
||||
|
||||
/// A list specifying general categories of I/O error.
|
||||
///
|
||||
/// This list is intended to grow over time and it is not recommended to
|
||||
/// exhaustively match against it.
|
||||
///
|
||||
/// It is used with the [`io::Error`] type.
|
||||
///
|
||||
/// [`io::Error`]: Error
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
// #[allow(deprecated)]
|
||||
#[non_exhaustive]
|
||||
pub enum ErrorKind {
|
||||
/// An entity was not found, often a file.
|
||||
NotFound,
|
||||
/// The operation lacked the necessary privileges to complete.
|
||||
PermissionDenied,
|
||||
/// The connection was refused by the remote server.
|
||||
ConnectionRefused,
|
||||
/// The connection was reset by the remote server.
|
||||
ConnectionReset,
|
||||
/// The connection was aborted (terminated) by the remote server.
|
||||
ConnectionAborted,
|
||||
/// The network operation failed because it was not connected yet.
|
||||
NotConnected,
|
||||
/// A socket address could not be bound because the address is already in
|
||||
/// use elsewhere.
|
||||
AddrInUse,
|
||||
/// A nonexistent interface was requested or the requested address was not
|
||||
/// local.
|
||||
AddrNotAvailable,
|
||||
/// The operation failed because a pipe was closed.
|
||||
BrokenPipe,
|
||||
/// An entity already exists, often a file.
|
||||
AlreadyExists,
|
||||
/// The operation needs to block to complete, but the blocking operation was
|
||||
/// requested to not occur.
|
||||
WouldBlock,
|
||||
/// A parameter was incorrect.
|
||||
InvalidInput,
|
||||
/// Data not valid for the operation were encountered.
|
||||
///
|
||||
/// Unlike [`InvalidInput`], this typically means that the operation
|
||||
/// parameters were valid, however the error was caused by malformed
|
||||
/// input data.
|
||||
///
|
||||
/// For example, a function that reads a file into a string will error with
|
||||
/// `InvalidData` if the file's contents are not valid UTF-8.
|
||||
///
|
||||
/// [`InvalidInput`]: ErrorKind::InvalidInput
|
||||
InvalidData,
|
||||
/// The I/O operation's timeout expired, causing it to be canceled.
|
||||
TimedOut,
|
||||
/// An error returned when an operation could not be completed because a
|
||||
/// call to [`write`] returned [`Ok(0)`].
|
||||
///
|
||||
/// This typically means that an operation could only succeed if it wrote a
|
||||
/// particular number of bytes but only a smaller number of bytes could be
|
||||
/// written.
|
||||
///
|
||||
/// [`write`]: crate::io::Write::write
|
||||
/// [`Ok(0)`]: Ok
|
||||
WriteZero,
|
||||
/// This operation was interrupted.
|
||||
///
|
||||
/// Interrupted operations can typically be retried.
|
||||
Interrupted,
|
||||
/// Any I/O error not part of this list.
|
||||
///
|
||||
/// Errors that are `Other` now may move to a different or a new
|
||||
/// [`ErrorKind`] variant in the future. It is not recommended to match
|
||||
/// an error against `Other` and to expect any additional characteristics,
|
||||
/// e.g., a specific [`Error::raw_os_error`] return value.
|
||||
Other,
|
||||
|
||||
/// An error returned when an operation could not be completed because an
|
||||
/// "end of file" was reached prematurely.
|
||||
///
|
||||
/// This typically means that an operation could only succeed if it read a
|
||||
/// particular number of bytes but only a smaller number of bytes could be
|
||||
/// read.
|
||||
UnexpectedEof,
|
||||
|
||||
/// Any I/O error from the standard library that's not part of this list.
|
||||
///
|
||||
/// Errors that are `Uncategorized` now may move to a different or a new
|
||||
/// [`ErrorKind`] variant in the future. It is not recommended to match
|
||||
/// an error against `Uncategorized`; use a wildcard match (`_`) instead.
|
||||
#[doc(hidden)]
|
||||
Uncategorized,
|
||||
}
|
||||
|
||||
impl ErrorKind {
|
||||
pub(crate) fn as_str(&self) -> &'static str {
|
||||
match *self {
|
||||
ErrorKind::NotFound => "entity not found",
|
||||
ErrorKind::PermissionDenied => "permission denied",
|
||||
ErrorKind::ConnectionRefused => "connection refused",
|
||||
ErrorKind::ConnectionReset => "connection reset",
|
||||
ErrorKind::ConnectionAborted => "connection aborted",
|
||||
ErrorKind::NotConnected => "not connected",
|
||||
ErrorKind::AddrInUse => "address in use",
|
||||
ErrorKind::AddrNotAvailable => "address not available",
|
||||
ErrorKind::BrokenPipe => "broken pipe",
|
||||
ErrorKind::AlreadyExists => "entity already exists",
|
||||
ErrorKind::WouldBlock => "operation would block",
|
||||
ErrorKind::InvalidInput => "invalid input parameter",
|
||||
ErrorKind::InvalidData => "invalid data",
|
||||
ErrorKind::TimedOut => "timed out",
|
||||
ErrorKind::WriteZero => "write zero",
|
||||
ErrorKind::Interrupted => "operation interrupted",
|
||||
ErrorKind::Other => "other os error",
|
||||
ErrorKind::UnexpectedEof => "unexpected end of file",
|
||||
ErrorKind::Uncategorized => "uncategorized",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::io::ErrorKind> for ErrorKind {
|
||||
/// Converts an [`std::io::ErrorKind`] into an [`ErrorKind`].
|
||||
///
|
||||
/// This conversion allocates a new error with a simple representation of error kind.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// let not_found = ErrorKind::from(std::io::ErrorKind::NotFound);
|
||||
/// let err: Error = not_found.into();
|
||||
/// assert_eq!("entity not found", format!("{}", err));
|
||||
/// ```
|
||||
fn from(k: std::io::ErrorKind) -> Self {
|
||||
match k {
|
||||
std::io::ErrorKind::NotFound => ErrorKind::NotFound,
|
||||
std::io::ErrorKind::PermissionDenied => ErrorKind::PermissionDenied,
|
||||
std::io::ErrorKind::ConnectionRefused => ErrorKind::ConnectionRefused,
|
||||
std::io::ErrorKind::ConnectionReset => ErrorKind::ConnectionReset,
|
||||
std::io::ErrorKind::ConnectionAborted => ErrorKind::ConnectionAborted,
|
||||
std::io::ErrorKind::NotConnected => ErrorKind::NotConnected,
|
||||
std::io::ErrorKind::AddrInUse => ErrorKind::AddrInUse,
|
||||
std::io::ErrorKind::AddrNotAvailable => ErrorKind::AddrNotAvailable,
|
||||
std::io::ErrorKind::BrokenPipe => ErrorKind::BrokenPipe,
|
||||
std::io::ErrorKind::AlreadyExists => ErrorKind::AlreadyExists,
|
||||
std::io::ErrorKind::WouldBlock => ErrorKind::WouldBlock,
|
||||
std::io::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
|
||||
std::io::ErrorKind::InvalidData => ErrorKind::InvalidData,
|
||||
std::io::ErrorKind::TimedOut => ErrorKind::TimedOut,
|
||||
std::io::ErrorKind::WriteZero => ErrorKind::WriteZero,
|
||||
std::io::ErrorKind::Interrupted => ErrorKind::Interrupted,
|
||||
std::io::ErrorKind::Other => ErrorKind::Other,
|
||||
std::io::ErrorKind::UnexpectedEof => ErrorKind::UnexpectedEof,
|
||||
_ => ErrorKind::Uncategorized,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Intended for use for errors not exposed to the user, where allocating onto
|
||||
/// the heap (for normal construction via Error::new) is too costly.
|
||||
impl From<ErrorKind> for Error {
|
||||
/// Converts an [`ErrorKind`] into an [`Error`].
|
||||
///
|
||||
/// This conversion allocates a new error with a simple representation of error kind.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// let not_found = ErrorKind::NotFound;
|
||||
/// let error = Error::from(not_found);
|
||||
/// assert_eq!("entity not found", format!("{}", error));
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from(kind: ErrorKind) -> Error {
|
||||
Error {
|
||||
repr: Repr::Simple(kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl From<std::io::Error> for Error {
|
||||
/// Converts an [`std::io::ErrorKind`] into an [`Error`].
|
||||
///
|
||||
/// This conversion allocates a new error with a simple representation of error kind.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// let not_found = std::io::Error::from(std::io::ErrorKind::NotFound);
|
||||
/// let error = Error::from(not_found);
|
||||
/// assert_eq!("entity not found", format!("{}", error));
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Self::from(ErrorKind::from(err.kind()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Creates a new I/O error from a known kind of error as well as an
|
||||
/// arbitrary error payload.
|
||||
///
|
||||
/// This function is used to generically create I/O errors which do not
|
||||
/// originate from the OS itself. The `error` argument is an arbitrary
|
||||
/// payload which will be contained in this [`Error`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// // errors can be created from strings
|
||||
/// let custom_error = Error::new(ErrorKind::Other, "oh no!");
|
||||
///
|
||||
/// // errors can also be created from other errors
|
||||
/// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error.into_inner().unwrap());
|
||||
/// ```
|
||||
pub fn new(kind: ErrorKind, error: &'static str) -> Error {
|
||||
Self::_new(kind, error.into())
|
||||
}
|
||||
|
||||
fn _new(kind: ErrorKind, error: &'static str) -> Error {
|
||||
Error {
|
||||
repr: Repr::Custom(Custom { kind, error }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner error wrapped by this error (if any).
|
||||
///
|
||||
/// If this [`Error`] was constructed via [`new`] then this function will
|
||||
/// return [`Some`], otherwise it will return [`None`].
|
||||
///
|
||||
/// [`new`]: Error::new
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// fn print_error(err: &Error) {
|
||||
/// if let Some(inner_err) = err.get_ref() {
|
||||
/// println!("Inner error: {:?}", inner_err);
|
||||
/// } else {
|
||||
/// println!("No inner error");
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[cfg(feature = "std")]
|
||||
/// fn emit_error() {
|
||||
/// // Will print "No inner error".
|
||||
/// print_error(&Error::from(std::io::Error::last_os_error()));
|
||||
/// // Will print "Inner error: ...".
|
||||
/// print_error(&Error::new(ErrorKind::Other, "oh no!"));
|
||||
/// }
|
||||
///
|
||||
/// #[cfg(not(feature = "std"))]
|
||||
/// fn emit_error() {
|
||||
/// // Will print "No inner error".
|
||||
/// print_error(&ErrorKind::Other.into());
|
||||
/// // Will print "Inner error: ...".
|
||||
/// print_error(&Error::new(ErrorKind::Other, "oh no!"));
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// emit_error();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn get_ref(&self) -> Option<&&'static str> {
|
||||
match self.repr {
|
||||
Repr::Simple(..) => None,
|
||||
Repr::Custom(ref c) => Some(&c.error),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes the `Error`, returning its inner error (if any).
|
||||
///
|
||||
/// If this [`Error`] was constructed via [`new`] then this function will
|
||||
/// return [`Some`], otherwise it will return [`None`].
|
||||
///
|
||||
/// [`new`]: Error::new
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// fn print_error(err: Error) {
|
||||
/// if let Some(inner_err) = err.into_inner() {
|
||||
/// println!("Inner error: {}", inner_err);
|
||||
/// } else {
|
||||
/// println!("No inner error");
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// #[cfg(feature = "std")]
|
||||
/// fn emit_error() {
|
||||
/// // Will print "No inner error".
|
||||
/// print_error(std::io::Error::last_os_error().into());
|
||||
/// // Will print "Inner error: ...".
|
||||
/// print_error(Error::new(ErrorKind::Other, "oh no!"));
|
||||
/// }
|
||||
///
|
||||
/// #[cfg(not(feature = "std"))]
|
||||
/// fn emit_error() {
|
||||
/// // Will print "No inner error".
|
||||
/// print_error(ErrorKind::Other.into());
|
||||
/// // Will print "Inner error: ...".
|
||||
/// print_error(Error::new(ErrorKind::Other, "oh no!"));
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// emit_error();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn into_inner(self) -> Option<&'static str> {
|
||||
match self.repr {
|
||||
Repr::Simple(..) => None,
|
||||
Repr::Custom(c) => Some(c.error),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the corresponding [`ErrorKind`] for this error.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use core2::io::{Error, ErrorKind};
|
||||
///
|
||||
/// fn print_error(err: Error) {
|
||||
/// println!("{:?}", err.kind());
|
||||
/// }
|
||||
///
|
||||
/// #[cfg(feature = "std")]
|
||||
/// fn emit_error() {
|
||||
/// // Will print "Other".
|
||||
/// print_error(std::io::Error::last_os_error().into());
|
||||
/// // Will print "AddrInUse".
|
||||
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
|
||||
/// }
|
||||
///
|
||||
/// #[cfg(not(feature = "std"))]
|
||||
/// fn emit_error() {
|
||||
/// // Will print "Other".
|
||||
/// print_error(ErrorKind::Other.into());
|
||||
/// // Will print "AddrInUse".
|
||||
/// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// emit_error();
|
||||
/// }
|
||||
/// ```
|
||||
pub fn kind(&self) -> ErrorKind {
|
||||
match self.repr {
|
||||
Repr::Custom(ref c) => c.kind,
|
||||
Repr::Simple(kind) => kind,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Repr {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
|
||||
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.repr {
|
||||
Repr::Custom(ref c) => c.error.fmt(fmt),
|
||||
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn _assert_error_is_sync_send() {
|
||||
fn _is_sync_send<T: Sync + Send>() {}
|
||||
_is_sync_send::<Error>();
|
||||
}
|
||||
Vendored
+176
@@ -0,0 +1,176 @@
|
||||
use super::error::{Error, ErrorKind, Result};
|
||||
use super::traits::{BufRead, Read, Seek, SeekFrom, Write};
|
||||
use core::{cmp, fmt, mem};
|
||||
|
||||
// =============================================================================
|
||||
// Forwarding implementations
|
||||
|
||||
impl<R: Read + ?Sized> Read for &mut R {
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
(**self).read(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
|
||||
(**self).read_exact(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write + ?Sized> Write for &mut W {
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||
(**self).write(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<()> {
|
||||
(**self).flush()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
|
||||
(**self).write_all(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
|
||||
(**self).write_fmt(fmt)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Seek + ?Sized> Seek for &mut S {
|
||||
#[inline]
|
||||
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
|
||||
(**self).seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: BufRead + ?Sized> BufRead for &mut B {
|
||||
#[inline]
|
||||
fn fill_buf(&mut self) -> Result<&[u8]> {
|
||||
(**self).fill_buf()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn consume(&mut self, amt: usize) {
|
||||
(**self).consume(amt)
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// In-memory buffer implementations
|
||||
|
||||
/// Read is implemented for `&[u8]` by copying from the slice.
|
||||
///
|
||||
/// Note that reading updates the slice to point to the yet unread part.
|
||||
/// The slice will be empty when EOF is reached.
|
||||
impl Read for &[u8] {
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let amt = cmp::min(buf.len(), self.len());
|
||||
let (a, b) = self.split_at(amt);
|
||||
|
||||
// First check if the amount of bytes we want to read is small:
|
||||
// `copy_from_slice` will generally expand to a call to `memcpy`, and
|
||||
// for a single byte the overhead is significant.
|
||||
if amt == 1 {
|
||||
buf[0] = a[0];
|
||||
} else {
|
||||
buf[..amt].copy_from_slice(a);
|
||||
}
|
||||
|
||||
*self = b;
|
||||
Ok(amt)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
|
||||
if buf.len() > self.len() {
|
||||
return Err(Error::new(
|
||||
ErrorKind::UnexpectedEof,
|
||||
"failed to fill whole buffer",
|
||||
));
|
||||
}
|
||||
let (a, b) = self.split_at(buf.len());
|
||||
|
||||
// First check if the amount of bytes we want to read is small:
|
||||
// `copy_from_slice` will generally expand to a call to `memcpy`, and
|
||||
// for a single byte the overhead is significant.
|
||||
if buf.len() == 1 {
|
||||
buf[0] = a[0];
|
||||
} else {
|
||||
buf.copy_from_slice(a);
|
||||
}
|
||||
|
||||
*self = b;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl BufRead for &[u8] {
|
||||
#[inline]
|
||||
fn fill_buf(&mut self) -> Result<&[u8]> {
|
||||
Ok(*self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn consume(&mut self, amt: usize) {
|
||||
*self = &self[amt..];
|
||||
}
|
||||
}
|
||||
|
||||
/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
|
||||
/// its data.
|
||||
///
|
||||
/// Note that writing updates the slice to point to the yet unwritten part.
|
||||
/// The slice will be empty when it has been completely overwritten.
|
||||
impl Write for &mut [u8] {
|
||||
#[inline]
|
||||
fn write(&mut self, data: &[u8]) -> Result<usize> {
|
||||
let amt = cmp::min(data.len(), self.len());
|
||||
let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
|
||||
a.copy_from_slice(&data[..amt]);
|
||||
*self = b;
|
||||
Ok(amt)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_all(&mut self, data: &[u8]) -> Result<()> {
|
||||
if self.write(data)? == data.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::new(
|
||||
ErrorKind::WriteZero,
|
||||
"failed to write whole buffer",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Write is implemented for `Vec<u8>` by appending to the vector.
|
||||
/// The vector will grow as needed.
|
||||
#[cfg(feature = "alloc")]
|
||||
impl Write for alloc::vec::Vec<u8> {
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||
self.extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
|
||||
self.extend_from_slice(buf);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
#[cfg(feature = "nightly")]
|
||||
mod buffered;
|
||||
mod cursor;
|
||||
mod error;
|
||||
mod impls;
|
||||
mod traits;
|
||||
mod util;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use cursor::Cursor;
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use error::{Error, ErrorKind, Result};
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use traits::{BufRead, Bytes, Chain, Read, Seek, SeekFrom, Take, Write};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::io::{
|
||||
BufRead, Bytes, Chain, Cursor, Error, ErrorKind, Read, Result, Seek, SeekFrom, Take, Write,
|
||||
};
|
||||
|
||||
// Use this crate's implementation on both std and no_std
|
||||
#[cfg(feature = "nightly")]
|
||||
pub use buffered::{BufReader, BufWriter, LineWriter};
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
pub use util::copy;
|
||||
Vendored
+1363
File diff suppressed because it is too large.
Load diff
Vendored
+40
@@ -0,0 +1,40 @@
|
||||
#[cfg(feature = "nightly")]
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
use crate::io::{ErrorKind, Read, Write};
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
pub fn copy<R: ?Sized, W: ?Sized, const S: usize>(
|
||||
reader: &mut R,
|
||||
writer: &mut W,
|
||||
) -> crate::io::Result<u64>
|
||||
where
|
||||
R: Read,
|
||||
W: Write,
|
||||
{
|
||||
let mut buf = MaybeUninit::<[u8; S]>::uninit();
|
||||
// FIXME: #42788
|
||||
//
|
||||
// - This creates a (mut) reference to a slice of
|
||||
// _uninitialized_ integers, which is **undefined behavior**
|
||||
//
|
||||
// - Only the standard library gets to soundly "ignore" this,
|
||||
// based on its privileged knowledge of unstable rustc
|
||||
// internals;
|
||||
unsafe {
|
||||
reader.initializer().initialize(buf.assume_init_mut());
|
||||
}
|
||||
|
||||
let mut written = 0;
|
||||
loop {
|
||||
let len = match reader.read(unsafe { buf.assume_init_mut() }) {
|
||||
Ok(0) => return Ok(written),
|
||||
Ok(len) => len,
|
||||
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
|
||||
written += len as u64;
|
||||
}
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
#![cfg_attr(feature = "nightly", feature(maybe_uninit_ref))]
|
||||
#![cfg_attr(feature = "nightly", feature(never_type))]
|
||||
#![cfg_attr(all(feature = "std", feature = "nightly"), feature(read_initializer))]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(feature = "std", allow(dead_code))]
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub mod error;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::error as error;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub mod io;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::io as io;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
Vendored
+501
@@ -0,0 +1,501 @@
|
||||
use core::cmp;
|
||||
use core2::io::{self as io, BufRead, Cursor, Read, Write};
|
||||
|
||||
// #[test]
|
||||
// #[cfg_attr(target_os = "emscripten", ignore)]
|
||||
// fn read_until() {
|
||||
// let mut buf = Cursor::new(&b"12"[..]);
|
||||
// let mut v = Vec::new();
|
||||
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 2);
|
||||
// assert_eq!(v, b"12");
|
||||
|
||||
// let mut buf = Cursor::new(&b"1233"[..]);
|
||||
// let mut v = Vec::new();
|
||||
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 3);
|
||||
// assert_eq!(v, b"123");
|
||||
// v.truncate(0);
|
||||
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 1);
|
||||
// assert_eq!(v, b"3");
|
||||
// v.truncate(0);
|
||||
// assert_eq!(buf.read_until(b'3', &mut v).unwrap(), 0);
|
||||
// assert_eq!(v, []);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn split() {
|
||||
// let buf = Cursor::new(&b"12"[..]);
|
||||
// let mut s = buf.split(b'3');
|
||||
// assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
|
||||
// assert!(s.next().is_none());
|
||||
|
||||
// let buf = Cursor::new(&b"1233"[..]);
|
||||
// let mut s = buf.split(b'3');
|
||||
// assert_eq!(s.next().unwrap().unwrap(), vec![b'1', b'2']);
|
||||
// assert_eq!(s.next().unwrap().unwrap(), vec![]);
|
||||
// assert!(s.next().is_none());
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn read_line() {
|
||||
// let mut buf = Cursor::new(&b"12"[..]);
|
||||
// let mut v = String::new();
|
||||
// assert_eq!(buf.read_line(&mut v).unwrap(), 2);
|
||||
// assert_eq!(v, "12");
|
||||
|
||||
// let mut buf = Cursor::new(&b"12\n\n"[..]);
|
||||
// let mut v = String::new();
|
||||
// assert_eq!(buf.read_line(&mut v).unwrap(), 3);
|
||||
// assert_eq!(v, "12\n");
|
||||
// v.truncate(0);
|
||||
// assert_eq!(buf.read_line(&mut v).unwrap(), 1);
|
||||
// assert_eq!(v, "\n");
|
||||
// v.truncate(0);
|
||||
// assert_eq!(buf.read_line(&mut v).unwrap(), 0);
|
||||
// assert_eq!(v, "");
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn lines() {
|
||||
// let buf = Cursor::new(&b"12\r"[..]);
|
||||
// let mut s = buf.lines();
|
||||
// assert_eq!(s.next().unwrap().unwrap(), "12\r".to_string());
|
||||
// assert!(s.next().is_none());
|
||||
|
||||
// let buf = Cursor::new(&b"12\r\n\n"[..]);
|
||||
// let mut s = buf.lines();
|
||||
// assert_eq!(s.next().unwrap().unwrap(), "12".to_string());
|
||||
// assert_eq!(s.next().unwrap().unwrap(), "".to_string());
|
||||
// assert!(s.next().is_none());
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn read_to_end() {
|
||||
// let mut c = Cursor::new(&b""[..]);
|
||||
// let mut v = Vec::new();
|
||||
// assert_eq!(c.read_to_end(&mut v).unwrap(), 0);
|
||||
// assert_eq!(v, []);
|
||||
|
||||
// let mut c = Cursor::new(&b"1"[..]);
|
||||
// let mut v = Vec::new();
|
||||
// assert_eq!(c.read_to_end(&mut v).unwrap(), 1);
|
||||
// assert_eq!(v, b"1");
|
||||
|
||||
// let cap = 1024 * 1024;
|
||||
// let data = (0..cap).map(|i| (i / 3) as u8).collect::<Vec<_>>();
|
||||
// let mut v = Vec::new();
|
||||
// let (a, b) = data.split_at(data.len() / 2);
|
||||
// assert_eq!(Cursor::new(a).read_to_end(&mut v).unwrap(), a.len());
|
||||
// assert_eq!(Cursor::new(b).read_to_end(&mut v).unwrap(), b.len());
|
||||
// assert_eq!(v, data);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn read_to_string() {
|
||||
// let mut c = Cursor::new(&b""[..]);
|
||||
// let mut v = String::new();
|
||||
// assert_eq!(c.read_to_string(&mut v).unwrap(), 0);
|
||||
// assert_eq!(v, "");
|
||||
|
||||
// let mut c = Cursor::new(&b"1"[..]);
|
||||
// let mut v = String::new();
|
||||
// assert_eq!(c.read_to_string(&mut v).unwrap(), 1);
|
||||
// assert_eq!(v, "1");
|
||||
|
||||
// let mut c = Cursor::new(&b"\xff"[..]);
|
||||
// let mut v = String::new();
|
||||
// assert!(c.read_to_string(&mut v).is_err());
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn read_exact() {
|
||||
let mut buf = [0; 4];
|
||||
|
||||
let mut c = Cursor::new(&b""[..]);
|
||||
assert_eq!(
|
||||
c.read_exact(&mut buf).unwrap_err().kind(),
|
||||
io::ErrorKind::UnexpectedEof
|
||||
);
|
||||
|
||||
let mut c = Cursor::new(&b"123"[..]).chain(Cursor::new(&b"456789"[..]));
|
||||
c.read_exact(&mut buf).unwrap();
|
||||
assert_eq!(&buf, b"1234");
|
||||
c.read_exact(&mut buf).unwrap();
|
||||
assert_eq!(&buf, b"5678");
|
||||
assert_eq!(
|
||||
c.read_exact(&mut buf).unwrap_err().kind(),
|
||||
io::ErrorKind::UnexpectedEof
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_exact_slice() {
|
||||
let mut buf = [0; 4];
|
||||
|
||||
let mut c = &b""[..];
|
||||
assert_eq!(
|
||||
c.read_exact(&mut buf).unwrap_err().kind(),
|
||||
io::ErrorKind::UnexpectedEof
|
||||
);
|
||||
|
||||
let mut c = &b"123"[..];
|
||||
assert_eq!(
|
||||
c.read_exact(&mut buf).unwrap_err().kind(),
|
||||
io::ErrorKind::UnexpectedEof
|
||||
);
|
||||
// make sure the optimized (early returning) method is being used
|
||||
assert_eq!(&buf, &[0; 4]);
|
||||
|
||||
let mut c = &b"1234"[..];
|
||||
c.read_exact(&mut buf).unwrap();
|
||||
assert_eq!(&buf, b"1234");
|
||||
|
||||
let mut c = &b"56789"[..];
|
||||
c.read_exact(&mut buf).unwrap();
|
||||
assert_eq!(&buf, b"5678");
|
||||
assert_eq!(c, b"9");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_eof() {
|
||||
struct R;
|
||||
|
||||
impl Read for R {
|
||||
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, ""))
|
||||
}
|
||||
}
|
||||
impl BufRead for R {
|
||||
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
||||
Err(io::Error::new(io::ErrorKind::Other, ""))
|
||||
}
|
||||
fn consume(&mut self, _amt: usize) {}
|
||||
}
|
||||
|
||||
let mut buf = [0; 1];
|
||||
assert_eq!(0, R.take(0).read(&mut buf).unwrap());
|
||||
assert_eq!(b"", R.take(0).fill_buf().unwrap());
|
||||
}
|
||||
|
||||
fn cmp_bufread<Br1: BufRead, Br2: BufRead>(mut br1: Br1, mut br2: Br2, exp: &[u8]) {
|
||||
let mut cat = Vec::new();
|
||||
loop {
|
||||
let consume = {
|
||||
let buf1 = br1.fill_buf().unwrap();
|
||||
let buf2 = br2.fill_buf().unwrap();
|
||||
let minlen = if buf1.len() < buf2.len() {
|
||||
buf1.len()
|
||||
} else {
|
||||
buf2.len()
|
||||
};
|
||||
assert_eq!(buf1[..minlen], buf2[..minlen]);
|
||||
cat.extend_from_slice(&buf1[..minlen]);
|
||||
minlen
|
||||
};
|
||||
if consume == 0 {
|
||||
break;
|
||||
}
|
||||
br1.consume(consume);
|
||||
br2.consume(consume);
|
||||
}
|
||||
assert_eq!(br1.fill_buf().unwrap().len(), 0);
|
||||
assert_eq!(br2.fill_buf().unwrap().len(), 0);
|
||||
assert_eq!(&cat[..], &exp[..])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chain_bufread() {
|
||||
let testdata = b"ABCDEFGHIJKL";
|
||||
let chain1 = (&testdata[..3])
|
||||
.chain(&testdata[3..6])
|
||||
.chain(&testdata[6..9])
|
||||
.chain(&testdata[9..]);
|
||||
let chain2 = (&testdata[..4])
|
||||
.chain(&testdata[4..8])
|
||||
.chain(&testdata[8..]);
|
||||
cmp_bufread(chain1, chain2, &testdata[..]);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn chain_zero_length_read_is_not_eof() {
|
||||
// let a = b"A";
|
||||
// let b = b"B";
|
||||
// let mut s = String::new();
|
||||
// let mut chain = (&a[..]).chain(&b[..]);
|
||||
// chain.read(&mut []).unwrap();
|
||||
// chain.read_to_string(&mut s).unwrap();
|
||||
// assert_eq!("AB", s);
|
||||
// }
|
||||
|
||||
// #[bench]
|
||||
// #[cfg_attr(target_os = "emscripten", ignore)]
|
||||
// fn bench_read_to_end(b: &mut test::Bencher) {
|
||||
// b.iter(|| {
|
||||
// let mut lr = repeat(1).take(10000000);
|
||||
// let mut vec = Vec::with_capacity(1024);
|
||||
// super::read_to_end(&mut lr, &mut vec)
|
||||
// });
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn seek_len() -> io::Result<()> {
|
||||
// let mut c = Cursor::new(vec![0; 15]);
|
||||
// assert_eq!(c.stream_len()?, 15);
|
||||
|
||||
// c.seek(SeekFrom::End(0))?;
|
||||
// let old_pos = c.stream_position()?;
|
||||
// assert_eq!(c.stream_len()?, 15);
|
||||
// assert_eq!(c.stream_position()?, old_pos);
|
||||
|
||||
// c.seek(SeekFrom::Start(7))?;
|
||||
// c.seek(SeekFrom::Current(2))?;
|
||||
// let old_pos = c.stream_position()?;
|
||||
// assert_eq!(c.stream_len()?, 15);
|
||||
// assert_eq!(c.stream_position()?, old_pos);
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn seek_position() -> io::Result<()> {
|
||||
// // All `asserts` are duplicated here to make sure the method does not
|
||||
// // change anything about the seek state.
|
||||
// let mut c = Cursor::new(vec![0; 15]);
|
||||
// assert_eq!(c.stream_position()?, 0);
|
||||
// assert_eq!(c.stream_position()?, 0);
|
||||
|
||||
// c.seek(SeekFrom::End(0))?;
|
||||
// assert_eq!(c.stream_position()?, 15);
|
||||
// assert_eq!(c.stream_position()?, 15);
|
||||
|
||||
// c.seek(SeekFrom::Start(7))?;
|
||||
// c.seek(SeekFrom::Current(2))?;
|
||||
// assert_eq!(c.stream_position()?, 9);
|
||||
// assert_eq!(c.stream_position()?, 9);
|
||||
|
||||
// c.seek(SeekFrom::End(-3))?;
|
||||
// c.seek(SeekFrom::Current(1))?;
|
||||
// c.seek(SeekFrom::Current(-5))?;
|
||||
// assert_eq!(c.stream_position()?, 8);
|
||||
// assert_eq!(c.stream_position()?, 8);
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// A simple example reader which uses the default implementation of
|
||||
// read_to_end.
|
||||
struct ExampleSliceReader<'a> {
|
||||
slice: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> Read for ExampleSliceReader<'a> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let len = cmp::min(self.slice.len(), buf.len());
|
||||
buf[..len].copy_from_slice(&self.slice[..len]);
|
||||
self.slice = &self.slice[len..];
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_read_to_end_capacity() -> io::Result<()> {
|
||||
// let input = &b"foo"[..];
|
||||
|
||||
// // read_to_end() generally needs to over-allocate, both for efficiency
|
||||
// // and so that it can distinguish EOF. Assert that this is the case
|
||||
// // with this simple ExampleSliceReader struct, which uses the default
|
||||
// // implementation of read_to_end. Even though vec1 is allocated with
|
||||
// // exactly enough capacity for the read, read_to_end will allocate more
|
||||
// // space here.
|
||||
// let mut vec1 = Vec::with_capacity(input.len());
|
||||
// ExampleSliceReader { slice: input }.read_to_end(&mut vec1)?;
|
||||
// assert_eq!(vec1.len(), input.len());
|
||||
// assert!(vec1.capacity() > input.len(), "allocated more");
|
||||
|
||||
// // However, std::io::Take includes an implementation of read_to_end
|
||||
// // that will not allocate when the limit has already been reached. In
|
||||
// // this case, vec2 never grows.
|
||||
// let mut vec2 = Vec::with_capacity(input.len());
|
||||
// ExampleSliceReader { slice: input }.take(input.len() as u64).read_to_end(&mut vec2)?;
|
||||
// assert_eq!(vec2.len(), input.len());
|
||||
// assert_eq!(vec2.capacity(), input.len(), "did not allocate more");
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn io_slice_mut_advance() {
|
||||
// let mut buf1 = [1; 8];
|
||||
// let mut buf2 = [2; 16];
|
||||
// let mut buf3 = [3; 8];
|
||||
// let mut bufs = &mut [
|
||||
// IoSliceMut::new(&mut buf1),
|
||||
// IoSliceMut::new(&mut buf2),
|
||||
// IoSliceMut::new(&mut buf3),
|
||||
// ][..];
|
||||
|
||||
// // Only in a single buffer..
|
||||
// bufs = IoSliceMut::advance(bufs, 1);
|
||||
// assert_eq!(bufs[0].deref(), [1; 7].as_ref());
|
||||
// assert_eq!(bufs[1].deref(), [2; 16].as_ref());
|
||||
// assert_eq!(bufs[2].deref(), [3; 8].as_ref());
|
||||
|
||||
// // Removing a buffer, leaving others as is.
|
||||
// bufs = IoSliceMut::advance(bufs, 7);
|
||||
// assert_eq!(bufs[0].deref(), [2; 16].as_ref());
|
||||
// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
|
||||
|
||||
// // Removing a buffer and removing from the next buffer.
|
||||
// bufs = IoSliceMut::advance(bufs, 18);
|
||||
// assert_eq!(bufs[0].deref(), [3; 6].as_ref());
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn io_slice_mut_advance_empty_slice() {
|
||||
// let empty_bufs = &mut [][..];
|
||||
// // Shouldn't panic.
|
||||
// IoSliceMut::advance(empty_bufs, 1);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn io_slice_mut_advance_beyond_total_length() {
|
||||
// let mut buf1 = [1; 8];
|
||||
// let mut bufs = &mut [IoSliceMut::new(&mut buf1)][..];
|
||||
|
||||
// // Going beyond the total length should be ok.
|
||||
// bufs = IoSliceMut::advance(bufs, 9);
|
||||
// assert!(bufs.is_empty());
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn io_slice_advance() {
|
||||
// let buf1 = [1; 8];
|
||||
// let buf2 = [2; 16];
|
||||
// let buf3 = [3; 8];
|
||||
// let mut bufs = &mut [IoSlice::new(&buf1), IoSlice::new(&buf2), IoSlice::new(&buf3)][..];
|
||||
|
||||
// // Only in a single buffer..
|
||||
// bufs = IoSlice::advance(bufs, 1);
|
||||
// assert_eq!(bufs[0].deref(), [1; 7].as_ref());
|
||||
// assert_eq!(bufs[1].deref(), [2; 16].as_ref());
|
||||
// assert_eq!(bufs[2].deref(), [3; 8].as_ref());
|
||||
|
||||
// // Removing a buffer, leaving others as is.
|
||||
// bufs = IoSlice::advance(bufs, 7);
|
||||
// assert_eq!(bufs[0].deref(), [2; 16].as_ref());
|
||||
// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
|
||||
|
||||
// // Removing a buffer and removing from the next buffer.
|
||||
// bufs = IoSlice::advance(bufs, 18);
|
||||
// assert_eq!(bufs[0].deref(), [3; 6].as_ref());
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn io_slice_advance_empty_slice() {
|
||||
// let empty_bufs = &mut [][..];
|
||||
// // Shouldn't panic.
|
||||
// IoSlice::advance(empty_bufs, 1);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn io_slice_advance_beyond_total_length() {
|
||||
// let buf1 = [1; 8];
|
||||
// let mut bufs = &mut [IoSlice::new(&buf1)][..];
|
||||
|
||||
// // Going beyond the total length should be ok.
|
||||
// bufs = IoSlice::advance(bufs, 9);
|
||||
// assert!(bufs.is_empty());
|
||||
// }
|
||||
|
||||
/// Create a new writer that reads from at most `n_bufs` and reads
|
||||
/// `per_call` bytes (in total) per call to write.
|
||||
fn test_writer() -> TestWriter {
|
||||
TestWriter {
|
||||
written: Vec::new(),
|
||||
per_call: 2,
|
||||
}
|
||||
}
|
||||
|
||||
struct TestWriter {
|
||||
written: Vec<u8>,
|
||||
per_call: usize,
|
||||
}
|
||||
|
||||
impl Write for TestWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let n = self.per_call.min(buf.len());
|
||||
self.written.extend_from_slice(&buf[..n]);
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_writer_read_from_one_buf() {
|
||||
let mut writer = test_writer();
|
||||
|
||||
assert_eq!(writer.write(&[]).unwrap(), 0);
|
||||
// assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
|
||||
|
||||
// Read at most 2 bytes.
|
||||
assert_eq!(writer.write(&[1, 1, 1]).unwrap(), 2);
|
||||
assert_eq!(writer.write(&[2, 2, 2]).unwrap(), 2);
|
||||
|
||||
// Only read from first buf.
|
||||
assert_eq!(writer.write(&[3]).unwrap(), 1);
|
||||
|
||||
assert_eq!(writer.written, &[1, 1, 2, 2, 3]);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_writer_read_from_multiple_bufs() {
|
||||
// let mut writer = test_writer(3, 3);
|
||||
|
||||
// // Read at most 3 bytes from two buffers.
|
||||
// let bufs = &[IoSlice::new(&[1]), IoSlice::new(&[2, 2, 2])];
|
||||
// assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
|
||||
|
||||
// // Read at most 3 bytes from three buffers.
|
||||
// let bufs = &[IoSlice::new(&[3]), IoSlice::new(&[4]), IoSlice::new(&[5, 5])];
|
||||
// assert_eq!(writer.write_vectored(bufs).unwrap(), 3);
|
||||
|
||||
// assert_eq!(writer.written, &[1, 2, 2, 3, 4, 5]);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_write_all_vectored() {
|
||||
// #[rustfmt::skip] // Becomes unreadable otherwise.
|
||||
// let tests: Vec<(_, &'static [u8])> = vec![
|
||||
// (vec![], &[]),
|
||||
// (vec![IoSlice::new(&[]), IoSlice::new(&[])], &[]),
|
||||
// (vec![IoSlice::new(&[1])], &[1]),
|
||||
// (vec![IoSlice::new(&[1, 2])], &[1, 2]),
|
||||
// (vec![IoSlice::new(&[1, 2, 3])], &[1, 2, 3]),
|
||||
// (vec![IoSlice::new(&[1, 2, 3, 4])], &[1, 2, 3, 4]),
|
||||
// (vec![IoSlice::new(&[1, 2, 3, 4, 5])], &[1, 2, 3, 4, 5]),
|
||||
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2])], &[1, 2]),
|
||||
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2])], &[1, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2])], &[1, 1, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 2, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2])], &[1, 1, 1, 2, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 2, 2, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1, 1, 1, 1]), IoSlice::new(&[2, 2, 2, 2])], &[1, 1, 1, 1, 2, 2, 2, 2]),
|
||||
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2]), IoSlice::new(&[3])], &[1, 2, 3]),
|
||||
// (vec![IoSlice::new(&[1, 1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3])], &[1, 1, 2, 2, 3, 3]),
|
||||
// (vec![IoSlice::new(&[1]), IoSlice::new(&[2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 2, 2, 3, 3, 3]),
|
||||
// (vec![IoSlice::new(&[1, 1, 1]), IoSlice::new(&[2, 2, 2]), IoSlice::new(&[3, 3, 3])], &[1, 1, 1, 2, 2, 2, 3, 3, 3]),
|
||||
// ];
|
||||
|
||||
// let writer_configs = &[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)];
|
||||
|
||||
// for (n_bufs, per_call) in writer_configs.iter().copied() {
|
||||
// for (mut input, wanted) in tests.clone().into_iter() {
|
||||
// let mut writer = test_writer(n_bufs, per_call);
|
||||
// assert!(writer.write_all_vectored(&mut *input).is_ok());
|
||||
// assert_eq!(&*writer.written, &*wanted);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
Reference in new issue
Block a user