Skip to content

remove serialize::ebml, add librbml #16107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

TARGET_CRATES := libc std green rustuv native flate arena glob term semver \
uuid serialize sync getopts collections num test time rand \
url log regex graphviz core rlibc alloc debug rustrt \
url log regex graphviz core rbml rlibc alloc debug rustrt \
unicode
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros \
rustc_llvm rustc_back
Expand All @@ -71,7 +71,7 @@ DEPS_green := std native:context_switch
DEPS_rustuv := std native:uv native:uv_support
DEPS_native := std
DEPS_syntax := std term serialize log fmt_macros debug
DEPS_rustc := syntax flate arena serialize getopts \
DEPS_rustc := syntax flate arena serialize getopts rbml \
time log graphviz debug rustc_llvm rustc_back
DEPS_rustc_llvm := native:rustllvm libc std
DEPS_rustc_back := std syntax rustc_llvm flate log libc
Expand All @@ -82,6 +82,7 @@ DEPS_arena := std
DEPS_graphviz := std
DEPS_glob := std
DEPS_serialize := std log
DEPS_rbml := std log serialize
DEPS_term := std log
DEPS_semver := std
DEPS_uuid := std serialize
Expand All @@ -91,7 +92,7 @@ DEPS_collections := core alloc unicode
DEPS_fourcc := rustc syntax std
DEPS_hexfloat := rustc syntax std
DEPS_num := std
DEPS_test := std getopts serialize term time regex native:rust_test_helpers
DEPS_test := std getopts serialize rbml term time regex native:rust_test_helpers
DEPS_time := std serialize
DEPS_rand := core
DEPS_url := std
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/util/io.rs → src/librbml/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64>
///
/// ```rust
/// # #![allow(unused_must_use)]
/// use std::io::SeekableMemWriter;
/// use rbml::io::SeekableMemWriter;
///
/// let mut w = SeekableMemWriter::new();
/// w.write([0, 1, 2]);
Expand Down Expand Up @@ -128,6 +128,7 @@ impl Seek for SeekableMemWriter {

#[cfg(test)]
mod tests {
extern crate test;
use super::SeekableMemWriter;
use std::io;
use test::Bencher;
Expand Down
172 changes: 38 additions & 134 deletions src/libserialize/ebml.rs → src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,35 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Really Bad Markup Language (rbml) is a temporary measure until we migrate
//! the rust object metadata to a better serialization format. It is not
//! intended to be used by users.
//!
//! It is loosely based on the Extensible Binary Markup Language (ebml):
//! http://www.matroska.org/technical/specs/rfc/index.html

#![crate_name = "rbml"]
#![experimental]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![license = "MIT/ASL2"]
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://doc.rust-lang.org/master/",
html_playground_url = "http://play.rust-lang.org/")]
#![feature(macro_rules, phase)]
#![allow(missing_doc)]

use std::io;
extern crate serialize;

#[phase(plugin, link)] extern crate log;
#[cfg(test)] extern crate test;

use std::str;

// Simple Extensible Binary Markup Language (ebml) reader and writer on a
// cursor model. See the specification here:
// http://www.matroska.org/technical/specs/rfc/index.html
pub mod io;

// Common data structures
/// Common data structures
#[deriving(Clone)]
pub struct Doc<'a> {
pub data: &'a [u8],
Expand Down Expand Up @@ -86,7 +105,7 @@ pub enum EbmlEncoderTag {
pub enum Error {
IntTooBig(uint),
Expected(String),
IoError(io::IoError)
IoError(std::io::IoError)
}
// --------------------------------------

Expand All @@ -107,7 +126,7 @@ pub mod reader {
Expected };

pub type DecodeResult<T> = Result<T, Error>;
// ebml reading
// rbml reading

macro_rules! try_or(
($e:expr, $r:expr) => (
Expand Down Expand Up @@ -637,7 +656,7 @@ pub mod writer {

pub type EncodeResult = io::IoResult<()>;

// ebml writing
// rbml writing
pub struct Encoder<'a, W> {
pub writer: &'a mut W,
size_positions: Vec<uint>,
Expand Down Expand Up @@ -671,7 +690,7 @@ pub mod writer {
})
}

// FIXME (#2741): Provide a function to write the standard ebml header.
// FIXME (#2741): Provide a function to write the standard rbml header.
impl<'a, W: Writer + Seek> Encoder<'a, W> {
pub fn new(w: &'a mut W) -> Encoder<'a, W> {
Encoder {
Expand Down Expand Up @@ -1018,130 +1037,16 @@ pub mod writer {

#[cfg(test)]
mod tests {
use super::Doc;
use ebml::reader;
use ebml::writer;
use {Encodable, Decodable};
use super::{Doc, reader, writer};
use super::io::SeekableMemWriter;

use serialize::{Encodable, Decodable};

use std::io::{IoError, IoResult, SeekStyle};
use std::io;
use std::option::{None, Option, Some};
use std::slice;

static BUF_CAPACITY: uint = 128;

fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64> {
// compute offset as signed and clamp to prevent overflow
let pos = match seek {
io::SeekSet => 0,
io::SeekEnd => end,
io::SeekCur => cur,
} as i64;

if offset + pos < 0 {
Err(IoError {
kind: io::InvalidInput,
desc: "invalid seek to a negative offset",
detail: None
})
} else {
Ok((offset + pos) as u64)
}
}

/// Writes to an owned, growable byte vector that supports seeking.
///
/// # Example
///
/// ```rust
/// # #![allow(unused_must_use)]
/// use std::io::SeekableMemWriter;
///
/// let mut w = SeekableMemWriter::new();
/// w.write([0, 1, 2]);
///
/// assert_eq!(w.unwrap(), vec!(0, 1, 2));
/// ```
pub struct SeekableMemWriter {
buf: Vec<u8>,
pos: uint,
}

impl SeekableMemWriter {
/// Create a new `SeekableMemWriter`.
#[inline]
pub fn new() -> SeekableMemWriter {
SeekableMemWriter::with_capacity(BUF_CAPACITY)
}
/// Create a new `SeekableMemWriter`, allocating at least `n` bytes for
/// the internal buffer.
#[inline]
pub fn with_capacity(n: uint) -> SeekableMemWriter {
SeekableMemWriter { buf: Vec::with_capacity(n), pos: 0 }
}

/// Acquires an immutable reference to the underlying buffer of this
/// `SeekableMemWriter`.
///
/// No method is exposed for acquiring a mutable reference to the buffer
/// because it could corrupt the state of this `MemWriter`.
#[inline]
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }

/// Unwraps this `SeekableMemWriter`, returning the underlying buffer
#[inline]
pub fn unwrap(self) -> Vec<u8> { self.buf }
}

impl Writer for SeekableMemWriter {
#[inline]
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
if self.pos == self.buf.len() {
self.buf.push_all(buf)
} else {
// Make sure the internal buffer is as least as big as where we
// currently are
let difference = self.pos as i64 - self.buf.len() as i64;
if difference > 0 {
self.buf.grow(difference as uint, &0);
}

// Figure out what bytes will be used to overwrite what's currently
// there (left), and what will be appended on the end (right)
let cap = self.buf.len() - self.pos;
let (left, right) = if cap <= buf.len() {
(buf.slice_to(cap), buf.slice_from(cap))
} else {
(buf, &[])
};

// Do the necessary writes
if left.len() > 0 {
slice::bytes::copy_memory(self.buf.mut_slice_from(self.pos), left);
}
if right.len() > 0 {
self.buf.push_all(right);
}
}

// Bump us forward
self.pos += buf.len();
Ok(())
}
}

impl Seek for SeekableMemWriter {
#[inline]
fn tell(&self) -> IoResult<u64> { Ok(self.pos as u64) }

#[inline]
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
let new = try!(combine(style, self.pos, self.buf.len(), pos));
self.pos = new as uint;
Ok(())
}
}

#[test]
fn test_vuint_at() {
let data = [
Expand Down Expand Up @@ -1196,11 +1101,11 @@ mod tests {
debug!("v == {}", v);
let mut wr = SeekableMemWriter::new();
{
let mut ebml_w = writer::Encoder::new(&mut wr);
let _ = v.encode(&mut ebml_w);
let mut rbml_w = writer::Encoder::new(&mut wr);
let _ = v.encode(&mut rbml_w);
}
let ebml_doc = Doc::new(wr.get_ref());
let mut deser = reader::Decoder::new(ebml_doc);
let rbml_doc = Doc::new(wr.get_ref());
let mut deser = reader::Decoder::new(rbml_doc);
let v1 = Decodable::decode(&mut deser).unwrap();
debug!("v1 == {}", v1);
assert_eq!(v, v1);
Expand All @@ -1215,9 +1120,8 @@ mod tests {
#[cfg(test)]
mod bench {
#![allow(non_snake_case_functions)]
extern crate test;
use self::test::Bencher;
use ebml::reader;
use test::Bencher;
use super::reader;

#[bench]
pub fn vuint_at_A_aligned(b: &mut Bencher) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern crate libc;
extern crate llvm = "rustc_llvm";
extern crate rustc_back = "rustc_back";
extern crate serialize;
extern crate rbml;
extern crate time;
#[phase(plugin, link)] extern crate log;
#[phase(plugin, link)] extern crate syntax;
Expand Down Expand Up @@ -132,7 +133,6 @@ pub mod util {

pub mod common;
pub mod ppaux;
pub mod io;
pub mod nodemap;
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use middle::ty;
use middle::typeck;
use middle::subst::VecPerParamSpace;

use serialize::ebml;
use serialize::ebml::reader;
use rbml;
use rbml::reader;
use std::rc::Rc;
use syntax::ast;
use syntax::ast_map;
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
def: ast::DefId) -> ty::Polytype {
let cstore = &tcx.sess.cstore;
let cdata = cstore.get_crate_data(class_id.krate);
let all_items = reader::get_doc(ebml::Doc::new(cdata.data()), tag_items);
let all_items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_items);
let class_doc = expect(tcx.sess.diagnostic(),
decoder::maybe_find_item(class_id.node, all_items),
|| {
Expand Down
Loading