feat(tvix/nix-compat/nar/writer/sync): trait objects -> generics
This will later be used to allow the user to access the original writer. Change-Id: I1734600c5d580d18b16727f892cd2f6335cc9459 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12442 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
parent
cec28377d8
commit
863146295b
1 changed files with 10 additions and 13 deletions
|
@ -35,11 +35,8 @@ use std::io::{
|
||||||
Write,
|
Write,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Convenience type alias for types implementing [`Write`].
|
|
||||||
pub type Writer<'a> = dyn Write + Send + 'a;
|
|
||||||
|
|
||||||
/// Create a new NAR, writing the output to the specified writer.
|
/// Create a new NAR, writing the output to the specified writer.
|
||||||
pub fn open<'a, 'w: 'a>(writer: &'a mut Writer<'w>) -> io::Result<Node<'a, 'w>> {
|
pub fn open<W: Write>(writer: &mut W) -> io::Result<Node<W>> {
|
||||||
let mut node = Node { writer };
|
let mut node = Node { writer };
|
||||||
node.write(&wire::TOK_NAR)?;
|
node.write(&wire::TOK_NAR)?;
|
||||||
Ok(node)
|
Ok(node)
|
||||||
|
@ -49,11 +46,11 @@ pub fn open<'a, 'w: 'a>(writer: &'a mut Writer<'w>) -> io::Result<Node<'a, 'w>>
|
||||||
///
|
///
|
||||||
/// A NAR can be thought of as a tree of nodes represented by this type. Each
|
/// A NAR can be thought of as a tree of nodes represented by this type. Each
|
||||||
/// node can be a file, a symlink or a directory containing other nodes.
|
/// node can be a file, a symlink or a directory containing other nodes.
|
||||||
pub struct Node<'a, 'w: 'a> {
|
pub struct Node<'a, W: Write> {
|
||||||
writer: &'a mut Writer<'w>,
|
writer: &'a mut W,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> Node<'a, 'w> {
|
impl<'a, W: Write> Node<'a, W> {
|
||||||
fn write(&mut self, data: &[u8]) -> io::Result<()> {
|
fn write(&mut self, data: &[u8]) -> io::Result<()> {
|
||||||
self.writer.write_all(data)
|
self.writer.write_all(data)
|
||||||
}
|
}
|
||||||
|
@ -128,7 +125,7 @@ impl<'a, 'w> Node<'a, 'w> {
|
||||||
///
|
///
|
||||||
/// It is the caller's responsibility to invoke [`Directory::close`],
|
/// It is the caller's responsibility to invoke [`Directory::close`],
|
||||||
/// or invalid archives will be produced silently.
|
/// or invalid archives will be produced silently.
|
||||||
pub fn directory(mut self) -> io::Result<Directory<'a, 'w>> {
|
pub fn directory(mut self) -> io::Result<Directory<'a, W>> {
|
||||||
self.write(&wire::TOK_DIR)?;
|
self.write(&wire::TOK_DIR)?;
|
||||||
Ok(Directory::new(self))
|
Ok(Directory::new(self))
|
||||||
}
|
}
|
||||||
|
@ -145,13 +142,13 @@ fn into_name(_name: &[u8]) -> Name {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Content of a NAR node that represents a directory.
|
/// Content of a NAR node that represents a directory.
|
||||||
pub struct Directory<'a, 'w> {
|
pub struct Directory<'a, W: Write> {
|
||||||
node: Node<'a, 'w>,
|
node: Node<'a, W>,
|
||||||
prev_name: Option<Name>,
|
prev_name: Option<Name>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> Directory<'a, 'w> {
|
impl<'a, W: Write> Directory<'a, W> {
|
||||||
fn new(node: Node<'a, 'w>) -> Self {
|
fn new(node: Node<'a, W>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
node,
|
node,
|
||||||
prev_name: None,
|
prev_name: None,
|
||||||
|
@ -166,7 +163,7 @@ impl<'a, 'w> Directory<'a, 'w> {
|
||||||
/// It is the caller's responsibility to ensure that directory entries are
|
/// It is the caller's responsibility to ensure that directory entries are
|
||||||
/// written in order of ascending name. If this is not ensured, this method
|
/// written in order of ascending name. If this is not ensured, this method
|
||||||
/// may panic or silently produce invalid archives.
|
/// may panic or silently produce invalid archives.
|
||||||
pub fn entry(&mut self, name: &[u8]) -> io::Result<Node<'_, 'w>> {
|
pub fn entry(&mut self, name: &[u8]) -> io::Result<Node<'_, W>> {
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
name.len() <= wire::MAX_NAME_LEN,
|
name.len() <= wire::MAX_NAME_LEN,
|
||||||
"name.len() > {}",
|
"name.len() > {}",
|
||||||
|
|
Loading…
Reference in a new issue