Skip to content

Commit 9b1801c

Browse files
committed
Adjust layout modifying data transfer ergonomics
1 parent 10009e3 commit 9b1801c

File tree

6 files changed

+254
-47
lines changed

6 files changed

+254
-47
lines changed

texel/src/buf.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,10 @@ mod tests {
19961996
n
19971997
});
19981998

1999-
let target = atomic.as_texels(U16).index(offset..).index(..3 * MAX_ALIGN / 2);
1999+
let target = atomic
2000+
.as_texels(U16)
2001+
.index(offset..)
2002+
.index(..3 * MAX_ALIGN / 2);
20002003
U16.store_atomic_slice(target, &data[..]);
20012004

20022005
let mut check = [0; 3 * MAX_ALIGN / 2];

texel/src/image.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! advised, probably very common, and the only 'supported' use-case).
1111
mod atomic;
1212
mod cell;
13+
mod data;
1314
mod raw;
14-
mod unaligned;
1515

1616
use core::{fmt, ops};
1717

@@ -21,12 +21,12 @@ use crate::layout::{
2121
Bytes, Decay, Layout, Mend, PlaneOf, Raster, RasterMut, Relocate, SliceLayout, Take, TryMend,
2222
};
2323
use crate::texel::MAX_ALIGN;
24-
use crate::{Texel, TexelBuffer};
24+
use crate::{BufferReuseError, Texel, TexelBuffer};
2525

2626
pub use crate::stride::{StridedBufferMut, StridedBufferRef};
2727
pub use atomic::{AtomicImage, AtomicImageRef};
2828
pub use cell::{CellImage, CellImageRef};
29-
pub use unaligned::{DataCells, DataMut, DataRef};
29+
pub use data::{DataCells, DataMut, DataRef};
3030

3131
/// A container of allocated bytes, parameterized over the layout.
3232
///
@@ -848,6 +848,19 @@ impl<'data, L> ImageMut<'data, L> {
848848
Some(image.into())
849849
}
850850

851+
/// Attempt to modify the layout to a new value, without modifying its type.
852+
///
853+
/// Returns an `Err` if the layout does not fit the underlying buffer. Otherwise returns `Ok`
854+
/// and overwrites the layout accordingly.
855+
///
856+
/// TODO: public name and provide a `set_capacity` for `L = Bytes`?
857+
pub(crate) fn try_set_layout(&mut self, layout: L) -> Result<(), BufferReuseError>
858+
where
859+
L: Layout,
860+
{
861+
self.inner.try_reuse(layout)
862+
}
863+
851864
/// Decay into a image with less specific layout.
852865
///
853866
/// See [`Image::decay`].

texel/src/image/atomic.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::buf::{atomic_buf, AtomicBuffer, AtomicSliceRef};
55
use crate::image::{raw::RawImage, IntoPlanesError};
66
use crate::layout::{Bytes, Decay, Layout, Mend, PlaneOf, Relocate, SliceLayout, Take, TryMend};
77
use crate::texel::{constants::U8, MAX_ALIGN};
8-
use crate::{Texel, TexelBuffer};
8+
use crate::{BufferReuseError, Texel, TexelBuffer};
99

1010
/// A container of allocated bytes, parameterized over the layout.
1111
///
@@ -77,6 +77,19 @@ impl<L: Layout> AtomicImage<L> {
7777
.map_err(Into::into)
7878
}
7979

80+
/// Attempt to modify the layout to a new value, without modifying its type.
81+
///
82+
/// Returns an `Err` if the layout does not fit the underlying buffer. Otherwise returns `Ok`
83+
/// and overwrites the layout accordingly.
84+
///
85+
/// TODO: public name and provide a `set_capacity` for `L = Bytes`?
86+
pub(crate) fn try_set_layout(&mut self, layout: L) -> Result<(), BufferReuseError>
87+
where
88+
L: Layout,
89+
{
90+
self.inner.try_reuse(layout)
91+
}
92+
8093
/// Decay into a image with less specific layout.
8194
///
8295
/// See the [`Decay`] trait for an explanation of this operation.
@@ -387,6 +400,19 @@ impl<'data, L> AtomicImageRef<'data, L> {
387400
Some(self.inner.try_reinterpret(layout).ok()?.into())
388401
}
389402

403+
/// Attempt to modify the layout to a new value, without modifying its type.
404+
///
405+
/// Returns an `Err` if the layout does not fit the underlying buffer. Otherwise returns `Ok`
406+
/// and overwrites the layout accordingly.
407+
///
408+
/// TODO: public name and provide a `set_capacity` for `L = Bytes`?
409+
pub(crate) fn try_set_layout(&mut self, layout: L) -> Result<(), BufferReuseError>
410+
where
411+
L: Layout,
412+
{
413+
self.inner.try_reuse(layout)
414+
}
415+
390416
/// Decay into a image with less specific layout.
391417
///
392418
/// See [`AtomicImage::decay`].

texel/src/image/cell.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::buf::{cell_buf, CellBuffer};
55
use crate::image::{raw::RawImage, IntoPlanesError};
66
use crate::layout::{Bytes, Decay, Layout, Mend, PlaneOf, Relocate, SliceLayout, Take, TryMend};
77
use crate::texel::{constants::U8, MAX_ALIGN};
8-
use crate::{Texel, TexelBuffer};
8+
use crate::{BufferReuseError, Texel, TexelBuffer};
99
use core::cell::Cell;
1010

1111
/// A container of allocated bytes, parameterized over the layout.
@@ -67,6 +67,19 @@ impl<L: Layout> CellImage<L> {
6767
.map_err(Into::into)
6868
}
6969

70+
/// Attempt to modify the layout to a new value, without modifying its type.
71+
///
72+
/// Returns an `Err` if the layout does not fit the underlying buffer. Otherwise returns `Ok`
73+
/// and overwrites the layout accordingly.
74+
///
75+
/// TODO: public name and provide a `set_capacity` for `L = Bytes`?
76+
pub(crate) fn try_set_layout(&mut self, layout: L) -> Result<(), BufferReuseError>
77+
where
78+
L: Layout,
79+
{
80+
self.inner.try_reuse(layout)
81+
}
82+
7083
/// Decay into a image with less specific layout.
7184
///
7285
/// See the [`Decay`] trait for an explanation of this operation.
@@ -370,6 +383,19 @@ impl<'data, L> CellImageRef<'data, L> {
370383
Some(self.inner.try_reinterpret(layout).ok()?.into())
371384
}
372385

386+
/// Attempt to modify the layout to a new value, without modifying its type.
387+
///
388+
/// Returns an `Err` if the layout does not fit the underlying buffer. Otherwise returns `Ok`
389+
/// and overwrites the layout accordingly.
390+
///
391+
/// TODO: public name and provide a `set_capacity` for `L = Bytes`?
392+
pub(crate) fn try_set_layout(&mut self, layout: L) -> Result<(), BufferReuseError>
393+
where
394+
L: Layout,
395+
{
396+
self.inner.try_reuse(layout)
397+
}
398+
373399
/// Decay into a image with less specific layout.
374400
///
375401
/// See [`CellImage::decay`].

0 commit comments

Comments
 (0)