Skip to content

Commit c318b90

Browse files
committed
Finish documenatation of the missing state features
1 parent 30573a0 commit c318b90

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

state/src/path.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@
132132
//! }
133133
//! }
134134
//! ```
135+
//!
136+
//! # A note on availability
137+
//!
138+
//! Originally, these path handling features are also meant to be usable outside of the context of `save` and `restore`, for example to create a temporary audio file. However, the specification does not define whether the `FreePath` feature only deallocates the path string or if it deallocates the files pointed by the path too. Therefore, we can not guarantee that files and strings live outside of the scope of a trait function call and had to restrict the usage to `save` and `restore`.
135139
use crate::StateErr;
136140
use lv2_core::feature::Feature;
137141
use lv2_core::prelude::*;
@@ -145,7 +149,11 @@ use std::rc::Rc;
145149
use std::sync::Mutex;
146150
use urid::*;
147151

148-
/// A host feature to make absolute paths.
152+
/// A host feature that allocates absolute file paths.
153+
///
154+
/// This is only useful in conjunction with the [`FreePath`](struct.FreePath.html) and [`MapPath`](struct.MapPath.html) features. Therefore, the interface of this feature is private and only exposed by a [`PathManager`](struct.PathManager.html), which is constructed from these three host features.
155+
///
156+
/// Please take a look at the [module documentation](index.html) for a usage example.
149157
pub struct MakePath<'a> {
150158
handle: sys::LV2_State_Make_Path_Handle,
151159
function: unsafe extern "C" fn(sys::LV2_State_Make_Path_Handle, *const c_char) -> *mut c_char,
@@ -193,7 +201,11 @@ impl<'a> MakePath<'a> {
193201
}
194202
}
195203

196-
/// A host feature to save and restore files.
204+
/// A host feature that maps absolute file paths to and from abstract file paths.
205+
///
206+
/// This is only useful in conjunction with the [`FreePath`](struct.FreePath.html) and [`MakePath`](struct.MakePath.html) features. Therefore, the interface of this feature is private and only exposed by a [`PathManager`](struct.PathManager.html), which is constructed from these three host features.
207+
///
208+
/// Please take a look at the [module documentation](index.html) for a usage example.
197209
pub struct MapPath<'a> {
198210
handle: sys::LV2_State_Map_Path_Handle,
199211
abstract_path: unsafe extern "C" fn(
@@ -263,7 +275,11 @@ impl<'a> MapPath<'a> {
263275
}
264276
}
265277

266-
/// A host feature to a previously allocated path.
278+
/// A host feature that deallocates absolute and abstract file paths.
279+
///
280+
/// This is only useful in conjunction with the [`MapPath`](struct.MapPath.html) and [`MakePath`](struct.MakePath.html) features. Therefore, the interface of this feature is private and only exposed by a [`PathManager`](struct.PathManager.html), which is constructed from these three host features.
281+
///
282+
/// Please take a look at the [module documentation](index.html) for a usage example.
267283
pub struct FreePath<'a> {
268284
handle: sys::LV2_State_Free_Path_Handle,
269285
free_path: unsafe extern "C" fn(sys::LV2_State_Free_Path_Handle, *mut c_char),
@@ -294,6 +310,11 @@ impl<'a> FreePath<'a> {
294310
}
295311
}
296312

313+
/// A path that has been allocated by the host.
314+
///
315+
/// An instance of this struct can be used just like a [`Path`](https://doc.rust-lang.org/stable/std/path/struct.Path.html) instance.
316+
///
317+
/// This path has been allocated by the host via a [`PathManager`](struct.PathManager.html) and will be deallocated by the host when dropped. Since it contains an `Rc<Mutex<>>`, it is neither `Send` nor `Sync` and shouldn't be used outside of the scope of a [`save`](../trait.State.html#tymethod.save) or [`restore`](../trait.State.html#tymethod.restore) call.
297318
pub struct ManagedPath<'a> {
298319
path: &'a Path,
299320
free_path: Rc<Mutex<FreePath<'a>>>,
@@ -322,6 +343,11 @@ impl<'a> Drop for ManagedPath<'a> {
322343
}
323344
}
324345

346+
/// A string that has been allocated by the host.
347+
///
348+
/// An instance of this struct can be used just like a [`str`](https://doc.rust-lang.org/stable/std/primitive.str.html) reference.
349+
///
350+
/// This string has been allocated by the host via a [`PathManager`](struct.PathManager.html) and will be deallocated by the host when dropped. Since it contains an `Rc<Mutex<>>`, it is neither `Send` nor `Sync` and shouldn't be used outside of the scope of a [`save`](../trait.State.html#tymethod.save) or [`restore`](../trait.State.html#tymethod.restore) call.
325351
pub struct ManagedStr<'a> {
326352
str: &'a str,
327353
free_path: Rc<Mutex<FreePath<'a>>>,
@@ -347,13 +373,19 @@ impl<'a> AsRef<str> for ManagedStr<'a> {
347373
}
348374
}
349375

376+
/// A safe interface to the path handling features.
377+
///
378+
/// This struct is constructed from the three path handling features, [`MakePath`](struct.MakePath.html), [`MapPath`](struct.MapPath.html), and [`FreePath`](struct.FreePath.html), and exposes them as one safe interface.
379+
///
380+
/// Please take a look at the [module documentation](index.html) for a usage example.
350381
pub struct PathManager<'a> {
351382
make: MakePath<'a>,
352383
map: MapPath<'a>,
353384
free: Rc<Mutex<FreePath<'a>>>,
354385
}
355386

356387
impl<'a> PathManager<'a> {
388+
/// Create a new path manager from the three path handling features.
357389
pub fn new(make: MakePath<'a>, map: MapPath<'a>, free: FreePath<'a>) -> Self {
358390
Self {
359391
make,
@@ -362,6 +394,13 @@ impl<'a> PathManager<'a> {
362394
}
363395
}
364396

397+
/// Allocate a new path.
398+
///
399+
/// This function maps the given relative file path to an absolute file path as well as an abstract file path. The absolute file path can be used to access the new file and the abstract file path is used to reference it in the state of the plugin. Storing the absolute file path in the plugin state will not work since it might have changed when the state is restored.
400+
///
401+
/// The relative file path will be the suffix of the absolute file path and will be contained in a namespace unique to the plugin instance. This means that allocations of the same relative path by different plugin instances will not collide. Apart from that, you can not make any other assumptions about the absolute and abstract file paths.
402+
///
403+
/// An abstract file path that has been read from the plugin state can be mapped back to an absolute file path with the [`deabstract_path`](#method.deabstract_path) method.
365404
pub fn allocate_path(
366405
&mut self,
367406
relative_path: &Path,
@@ -385,6 +424,9 @@ impl<'a> PathManager<'a> {
385424
Ok((absolute_path, abstract_path))
386425
}
387426

427+
/// Map an abstract file path back to an absolute one.
428+
///
429+
/// After reading an abstract file path from the state, you have to map it back to an absolute file path in order to read the file. This is what this method does.
388430
pub fn deabstract_path(&mut self, path: &str) -> Result<ManagedPath<'a>, StateErr> {
389431
self.map
390432
.abstract_to_absolute_path(path)

0 commit comments

Comments
 (0)