22
33//! Utilities for creating new [`DevicePaths`].
44//!
5- //! This module provides builders to construct device paths, as well as
6- //! submodules containing types for building each type of device path node.
7- //!
8- //! - [`DevicePathBuilder`] to construct device paths into pre-allocated buffers
9- //! - [`OwnedDevicePathBuilder`] for construction on the Rust heap
5+ //! This module contains [`DevicePathBuilder`], as well as submodules
6+ //! containing types for building each type of device path node.
107//!
118//! [`DevicePaths`]: DevicePath
129
@@ -18,10 +15,7 @@ use core::fmt::{self, Display, Formatter};
1815use core:: mem:: MaybeUninit ;
1916
2017#[ cfg( feature = "alloc" ) ]
21- use {
22- alloc:: { boxed:: Box , vec:: Vec } ,
23- core:: mem,
24- } ;
18+ use alloc:: vec:: Vec ;
2519
2620/// A builder for [`DevicePaths`].
2721///
@@ -80,14 +74,14 @@ use {
8074/// ```
8175#[ derive( Debug ) ]
8276pub struct DevicePathBuilder < ' a > {
83- storage : BuilderStorageRef < ' a > ,
77+ storage : BuilderStorage < ' a > ,
8478}
8579
8680impl < ' a > DevicePathBuilder < ' a > {
8781 /// Create a builder backed by a statically-sized buffer.
8882 pub const fn with_buf ( buf : & ' a mut [ MaybeUninit < u8 > ] ) -> Self {
8983 Self {
90- storage : BuilderStorageRef :: Buf { buf, offset : 0 } ,
84+ storage : BuilderStorage :: Buf { buf, offset : 0 } ,
9185 }
9286 }
9387
@@ -98,7 +92,7 @@ impl<'a> DevicePathBuilder<'a> {
9892 pub fn with_vec ( v : & ' a mut Vec < u8 > ) -> Self {
9993 v. clear ( ) ;
10094 Self {
101- storage : BuilderStorageRef :: Vec ( v) ,
95+ storage : BuilderStorage :: Vec ( v) ,
10296 }
10397 }
10498
@@ -113,15 +107,15 @@ impl<'a> DevicePathBuilder<'a> {
113107 let node_size = usize:: from ( node. size_in_bytes ( ) ?) ;
114108
115109 match & mut self . storage {
116- BuilderStorageRef :: Buf { buf, offset } => {
110+ BuilderStorage :: Buf { buf, offset } => {
117111 node. write_data (
118112 buf. get_mut ( * offset..* offset + node_size)
119113 . ok_or ( BuildError :: BufferTooSmall ) ?,
120114 ) ;
121115 * offset += node_size;
122116 }
123117 #[ cfg( feature = "alloc" ) ]
124- BuilderStorageRef :: Vec ( vec) => {
118+ BuilderStorage :: Vec ( vec) => {
125119 let old_size = vec. len ( ) ;
126120 vec. reserve ( node_size) ;
127121 let buf = & mut vec. spare_capacity_mut ( ) [ ..node_size] ;
@@ -144,11 +138,11 @@ impl<'a> DevicePathBuilder<'a> {
144138 let this = self . push ( & end:: Entire ) ?;
145139
146140 let data: & [ u8 ] = match & this. storage {
147- BuilderStorageRef :: Buf { buf, offset } => unsafe {
141+ BuilderStorage :: Buf { buf, offset } => unsafe {
148142 maybe_uninit_slice_assume_init_ref ( & buf[ ..* offset] )
149143 } ,
150144 #[ cfg( feature = "alloc" ) ]
151- BuilderStorageRef :: Vec ( vec) => vec,
145+ BuilderStorage :: Vec ( vec) => vec,
152146 } ;
153147
154148 let ptr: * const ( ) = data. as_ptr ( ) . cast ( ) ;
@@ -158,7 +152,7 @@ impl<'a> DevicePathBuilder<'a> {
158152
159153/// Reference to the backup storage for [`DevicePathBuilder`]
160154#[ derive( Debug ) ]
161- enum BuilderStorageRef < ' a > {
155+ enum BuilderStorage < ' a > {
162156 Buf {
163157 buf : & ' a mut [ MaybeUninit < u8 > ] ,
164158 offset : usize ,
@@ -200,58 +194,6 @@ impl Display for BuildError {
200194
201195impl core:: error:: Error for BuildError { }
202196
203- /// Variant of [`DevicePathBuilder`] to construct a boxed [`DevicePath`].
204- ///
205- /// Using this builder is equivalent to calling [`DevicePath::to_boxed`] on a
206- /// device path constructed by the normal builder.
207- #[ derive( Debug ) ]
208- #[ cfg( feature = "alloc" ) ]
209- pub struct OwnedDevicePathBuilder {
210- vec : Vec < u8 > ,
211- }
212-
213- #[ cfg( feature = "alloc" ) ]
214- impl OwnedDevicePathBuilder {
215- /// Creates a new builder.
216- pub fn new ( ) -> Self {
217- let vec = Vec :: new ( ) ;
218- Self { vec }
219- }
220-
221- /// Add a node to the device path.
222- ///
223- /// An error will be returned if an [`END_ENTIRE`] node is passed to
224- /// this function, as that node will be added when [`Self::finalize`] is
225- /// called.
226- ///
227- /// [`END_ENTIRE`]: uefi::proto::device_path::DeviceSubType::END_ENTIRE
228- pub fn push ( mut self , node : & dyn BuildNode ) -> Result < Self , BuildError > {
229- let node_size = usize:: from ( node. size_in_bytes ( ) ?) ;
230-
231- let old_size = self . vec . len ( ) ;
232- self . vec . reserve ( node_size) ;
233- let buf = & mut self . vec . spare_capacity_mut ( ) [ ..node_size] ;
234- node. write_data ( buf) ;
235- unsafe {
236- self . vec . set_len ( old_size + node_size) ;
237- }
238- Ok ( self )
239- }
240-
241- /// Add an [`END_ENTIRE`] node and return the resulting [`DevicePath`].
242- ///
243- /// This method consumes the builder.
244- ///
245- /// [`END_ENTIRE`]: uefi::proto::device_path::DeviceSubType::END_ENTIRE
246- pub fn finalize ( self ) -> Result < Box < DevicePath > , BuildError > {
247- let this = self . push ( & end:: Entire ) ?;
248- let boxed = this. vec . into_boxed_slice ( ) ;
249- // SAFETY: This is safe as a DevicePath has the same layout.
250- let dvp = unsafe { mem:: transmute ( boxed) } ;
251- Ok ( dvp)
252- }
253- }
254-
255197/// Trait for types that can be used to build a node via
256198/// [`DevicePathBuilder::push`].
257199///
0 commit comments