musikr: remove pinning for immutable refs

Not actually needed, can just use plain refs
This commit is contained in:
Alexander Capehart 2025-02-17 11:58:57 -07:00
parent 98bf82ea15
commit 1cb2f5026f
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
11 changed files with 116 additions and 125 deletions

View file

@ -12,18 +12,18 @@ impl<'file_ref> AudioProperties<'file_ref> {
}
pub fn length_in_milliseconds(&self) -> i32 {
self.this.pin().lengthInMilliseconds()
self.this.as_ref().lengthInMilliseconds()
}
pub fn bitrate(&self) -> i32 {
self.this.pin().bitrate()
self.this.as_ref().bitrate()
}
pub fn sample_rate(&self) -> i32 {
self.this.pin().sampleRate()
self.this.as_ref().sampleRate()
}
pub fn channels(&self) -> i32 {
self.this.pin().channels()
self.this.as_ref().channels()
}
}

View file

@ -46,13 +46,13 @@ mod bridge_impl {
#[cxx_name = "FileRef"]
type CPPFileRef;
unsafe fn new_FileRef(stream: *mut CPPIOStream) -> UniquePtr<CPPFileRef>;
fn isNull(self: Pin<&CPPFileRef>) -> bool;
fn file(self: Pin<&CPPFileRef>) -> *mut CPPFile;
fn isNull(self: &CPPFileRef) -> bool;
fn file(self: &CPPFileRef) -> *mut CPPFile;
#[namespace = "TagLib"]
#[cxx_name = "File"]
type CPPFile;
fn audioProperties(self: Pin<&CPPFile>) -> *mut CppAudioProperties;
fn audioProperties(self: &CPPFile) -> *mut CppAudioProperties;
#[namespace = "taglib_shim"]
unsafe fn File_asVorbis(file: *mut CPPFile) -> *mut CPPVorbisFile;
#[namespace = "taglib_shim"]
@ -69,16 +69,16 @@ mod bridge_impl {
#[namespace = "TagLib"]
#[cxx_name = "AudioProperties"]
type CppAudioProperties;
fn lengthInMilliseconds(self: Pin<&CppAudioProperties>) -> i32;
fn bitrate(self: Pin<&CppAudioProperties>) -> i32;
fn sampleRate(self: Pin<&CppAudioProperties>) -> i32;
fn channels(self: Pin<&CppAudioProperties>) -> i32;
fn lengthInMilliseconds(self: &CppAudioProperties) -> i32;
fn bitrate(self: &CppAudioProperties) -> i32;
fn sampleRate(self: &CppAudioProperties) -> i32;
fn channels(self: &CppAudioProperties) -> i32;
#[namespace = "TagLib::Ogg::Vorbis"]
#[cxx_name = "File"]
type CPPVorbisFile;
#[cxx_name = "tag"]
fn vorbisTag(self: Pin<&CPPVorbisFile>) -> *mut CPPXiphComment;
fn vorbisTag(self: &CPPVorbisFile) -> *mut CPPXiphComment;
#[namespace = "taglib_shim"]
fn XiphComment_pictureList(comment: Pin<&mut CPPXiphComment>) -> UniquePtr<CPPPictureList>;
@ -86,7 +86,7 @@ mod bridge_impl {
#[cxx_name = "File"]
type CPPOpusFile;
#[cxx_name = "tag"]
fn opusTag(self: Pin<&CPPOpusFile>) -> *mut CPPXiphComment;
fn opusTag(self: &CPPOpusFile) -> *mut CPPXiphComment;
#[namespace = "TagLib::FLAC"]
#[cxx_name = "File"]
@ -100,7 +100,7 @@ mod bridge_impl {
type CPPPictureList;
#[namespace = "taglib_shim"]
fn PictureList_to_vector(
list: Pin<&CPPPictureList>,
list: &CPPPictureList,
) -> UniquePtr<CxxVector<CPPFLACPicturePointer>>;
#[namespace = "taglib_shim"]
@ -125,41 +125,41 @@ mod bridge_impl {
#[cxx_name = "Picture"]
type CPPFLACPicture;
#[namespace = "taglib_shim"]
fn Picture_data(picture: Pin<&CPPFLACPicture>) -> UniquePtr<CPPByteVector>;
fn Picture_data(picture: &CPPFLACPicture) -> UniquePtr<CPPByteVector>;
#[namespace = "TagLib::Ogg"]
#[cxx_name = "XiphComment"]
type CPPXiphComment;
// Explicit lifecycle definition to state while the Pin is temporary, the CPPFieldListMap
// ref returned actually has the same lifetime as the CPPXiphComment.
fn fieldListMap<'slf, 'file_ref>(self: Pin<&'slf CPPXiphComment>) -> &'file_ref CPPFieldListMap;
fn fieldListMap<'slf, 'file_ref>(self: &'slf CPPXiphComment) -> &'file_ref CPPFieldListMap;
#[namespace = "TagLib"]
#[cxx_name = "SimplePropertyMap"]
type CPPFieldListMap;
#[namespace = "taglib_shim"]
fn FieldListMap_to_entries(
field_list_map: Pin<&CPPFieldListMap>,
field_list_map: &CPPFieldListMap,
) -> UniquePtr<CxxVector<CPPFieldListEntry>>;
#[namespace = "taglib_shim"]
#[cxx_name = "FieldListEntry"]
type CPPFieldListEntry;
fn key<'slf, 'file_ref>(self: Pin<&'slf CPPFieldListEntry>) -> &'file_ref CPPString;
fn value<'slf, 'file_ref>(self: Pin<&'slf CPPFieldListEntry>) -> &'file_ref CPPStringList;
fn key<'slf, 'file_ref>(self: &'slf CPPFieldListEntry) -> &'file_ref CPPString;
fn value<'slf, 'file_ref>(self: &'slf CPPFieldListEntry) -> &'file_ref CPPStringList;
#[namespace = "TagLib::ID3v2"]
#[cxx_name = "Tag"]
type CPPID3v2Tag;
#[namespace = "taglib_shim"]
fn Tag_frameList(tag: Pin<&CPPID3v2Tag>) -> UniquePtr<CPPID3v2FrameList>;
fn Tag_frameList(tag: &CPPID3v2Tag) -> UniquePtr<CPPID3v2FrameList>;
#[namespace = "TagLib::ID3v2"]
#[cxx_name = "FrameList"]
type CPPID3v2FrameList;
#[namespace = "taglib_shim"]
fn FrameList_to_vector(
list: Pin<&CPPID3v2FrameList>,
list: &CPPID3v2FrameList,
) -> UniquePtr<CxxVector<CPPFramePointer>>;
#[namespace = "taglib_shim"]
@ -188,7 +188,7 @@ mod bridge_impl {
type CPPID3v2TextIdentificationFrame;
#[namespace = "taglib_shim"]
fn TextIdentificationFrame_fieldList(
frame: Pin<&CPPID3v2TextIdentificationFrame>,
frame: &CPPID3v2TextIdentificationFrame,
) -> UniquePtr<CPPStringList>;
#[namespace = "TagLib::ID3v2"]
@ -196,7 +196,7 @@ mod bridge_impl {
type CPPID3v2UserTextIdentificationFrame;
#[namespace = "taglib_shim"]
fn UserTextIdentificationFrame_fieldList(
frame: Pin<&CPPID3v2UserTextIdentificationFrame>,
frame: &CPPID3v2UserTextIdentificationFrame,
) -> UniquePtr<CPPStringList>;
#[namespace = "TagLib::ID3v2"]
@ -204,45 +204,45 @@ mod bridge_impl {
type CPPID3v2AttachedPictureFrame;
#[namespace = "taglib_shim"]
fn AttachedPictureFrame_picture(
frame: Pin<&CPPID3v2AttachedPictureFrame>,
frame: &CPPID3v2AttachedPictureFrame,
) -> UniquePtr<CPPByteVector>;
#[namespace = "TagLib"]
#[cxx_name = "String"]
type CPPString;
fn toCString(self: Pin<&CPPString>, unicode: bool) -> *const c_char;
fn toCString(self: &CPPString, unicode: bool) -> *const c_char;
#[namespace = "TagLib"]
#[cxx_name = "StringList"]
type CPPStringList;
#[namespace = "taglib_shim"]
fn StringList_to_vector(
string_list: Pin<&CPPStringList>,
string_list: &CPPStringList,
) -> UniquePtr<CxxVector<CPPString>>;
#[namespace = "TagLib"]
#[cxx_name = "ByteVectorList"]
type CPPByteVectorList;
#[namespace = "taglib_shim"]
fn ByteVectorList_to_vector(list: Pin<&CPPByteVectorList>) -> UniquePtr<CxxVector<CPPByteVector>>;
fn ByteVectorList_to_vector(list: &CPPByteVectorList) -> UniquePtr<CxxVector<CPPByteVector>>;
#[namespace = "TagLib"]
#[cxx_name = "ByteVector"]
type CPPByteVector;
fn size(self: Pin<&CPPByteVector>) -> u32;
fn data(self: Pin<&CPPByteVector>) -> *const c_char;
fn size(self: &CPPByteVector) -> u32;
fn data(self: &CPPByteVector) -> *const c_char;
#[namespace = "TagLib::ID3v1"]
#[cxx_name = "Tag"]
type CPPID3v1Tag;
fn ID3v1Tag_title(tag: Pin<&CPPID3v1Tag>) -> UniquePtr<CPPString>;
fn ID3v1Tag_artist(tag: Pin<&CPPID3v1Tag>) -> UniquePtr<CPPString>;
fn ID3v1Tag_album(tag: Pin<&CPPID3v1Tag>) -> UniquePtr<CPPString>;
fn ID3v1Tag_comment(tag: Pin<&CPPID3v1Tag>) -> UniquePtr<CPPString>;
fn ID3v1Tag_genreIndex(tag: Pin<&CPPID3v1Tag>) -> u32;
fn ID3v1Tag_year(tag: Pin<&CPPID3v1Tag>) -> u32;
fn ID3v1Tag_track(tag: Pin<&CPPID3v1Tag>) -> u32;
fn ID3v1Tag_title(tag: &CPPID3v1Tag) -> UniquePtr<CPPString>;
fn ID3v1Tag_artist(tag: &CPPID3v1Tag) -> UniquePtr<CPPString>;
fn ID3v1Tag_album(tag: &CPPID3v1Tag) -> UniquePtr<CPPString>;
fn ID3v1Tag_comment(tag: &CPPID3v1Tag) -> UniquePtr<CPPString>;
fn ID3v1Tag_genreIndex(tag: &CPPID3v1Tag) -> u32;
fn ID3v1Tag_year(tag: &CPPID3v1Tag) -> u32;
fn ID3v1Tag_track(tag: &CPPID3v1Tag) -> u32;
#[namespace = "TagLib::MP4"]
#[cxx_name = "Tag"]
@ -251,52 +251,52 @@ mod bridge_impl {
#[namespace = "TagLib::MP4"]
#[cxx_name = "ItemMap"]
type CPPItemMap;
fn itemMap<'slf, 'file_ref>(self: Pin<&'slf CPPMP4Tag>) -> &'file_ref CPPItemMap;
fn ItemMap_to_entries(map: Pin<&CPPItemMap>) -> UniquePtr<CxxVector<CPPItemMapEntry>>;
fn itemMap<'slf, 'file_ref>(self: &'slf CPPMP4Tag) -> &'file_ref CPPItemMap;
fn ItemMap_to_entries(map: &CPPItemMap) -> UniquePtr<CxxVector<CPPItemMapEntry>>;
#[namespace = "taglib_shim"]
#[cxx_name = "ItemMapEntry"]
type CPPItemMapEntry;
fn key<'slf, 'file_ref>(self: Pin<&'slf CPPItemMapEntry>) -> &'file_ref CPPString;
fn value<'slf, 'file_ref>(self: Pin<&'slf CPPItemMapEntry>) -> &'file_ref CPPMP4Item;
fn key<'slf, 'file_ref>(self: &'slf CPPItemMapEntry) -> &'file_ref CPPString;
fn value<'slf, 'file_ref>(self: &'slf CPPItemMapEntry) -> &'file_ref CPPMP4Item;
#[namespace = "TagLib::MP4"]
#[cxx_name = "Item"]
type CPPMP4Item;
fn isValid(self: Pin<&CPPMP4Item>) -> bool;
fn toBool(self: Pin<&CPPMP4Item>) -> bool;
fn toInt(self: Pin<&CPPMP4Item>) -> i32;
fn toByte(self: Pin<&CPPMP4Item>) -> u8;
fn toUInt(self: Pin<&CPPMP4Item>) -> u32;
fn isValid(self: &CPPMP4Item) -> bool;
fn toBool(self: &CPPMP4Item) -> bool;
fn toInt(self: &CPPMP4Item) -> i32;
fn toByte(self: &CPPMP4Item) -> u8;
fn toUInt(self: &CPPMP4Item) -> u32;
fn Item_type(item: Pin<&CPPMP4Item>) -> u32;
fn Item_type(item: &CPPMP4Item) -> u32;
#[namespace = "taglib_shim"]
fn Item_toIntPair(item: Pin<&CPPMP4Item>) -> UniquePtr<CPPIntPair>;
fn Item_toIntPair(item: &CPPMP4Item) -> UniquePtr<CPPIntPair>;
#[namespace = "taglib_shim"]
fn Item_toStringList(item: Pin<&CPPMP4Item>) -> UniquePtr<CPPStringList>;
fn Item_toStringList(item: &CPPMP4Item) -> UniquePtr<CPPStringList>;
#[namespace = "taglib_shim"]
fn Item_toByteVectorList(item: Pin<&CPPMP4Item>) -> UniquePtr<CPPByteVectorList>;
fn Item_toByteVectorList(item: &CPPMP4Item) -> UniquePtr<CPPByteVectorList>;
#[namespace = "taglib_shim"]
fn Item_toCoverArtList(item: Pin<&CPPMP4Item>) -> UniquePtr<CPPCoverArtList>;
fn Item_toCoverArtList(item: &CPPMP4Item) -> UniquePtr<CPPCoverArtList>;
#[namespace = "taglib_shim"]
fn Item_toLongLong(item: Pin<&CPPMP4Item>) -> i64;
fn Item_toLongLong(item: &CPPMP4Item) -> i64;
#[namespace = "taglib_shim"]
#[cxx_name = "IntPair"]
type CPPIntPair;
fn first(self: Pin<&CPPIntPair>) -> i32;
fn second(self: Pin<&CPPIntPair>) -> i32;
fn first(self: &CPPIntPair) -> i32;
fn second(self: &CPPIntPair) -> i32;
#[namespace = "taglib_shim"]
#[cxx_name = "CoverArtList"]
type CPPCoverArtList;
fn to_vector(self: Pin<&CPPCoverArtList>) -> UniquePtr<CxxVector<CPPCoverArt>>;
fn to_vector(self: &CPPCoverArtList) -> UniquePtr<CxxVector<CPPCoverArt>>;
#[namespace = "taglib_shim"]
#[cxx_name = "CoverArt"]
type CPPCoverArt;
fn format(self: Pin<&CPPCoverArt>) -> u32;
fn data(self: Pin<&CPPCoverArt>) -> UniquePtr<CPPByteVector>;
fn format(self: &CPPCoverArt) -> u32;
fn data(self: &CPPCoverArt) -> UniquePtr<CPPByteVector>;
}
}

View file

@ -18,7 +18,7 @@ impl<'file_ref> File<'file_ref> {
}
pub fn audio_properties(&self) -> Option<AudioProperties<'file_ref>> {
let props_ptr = self.this.pin().audioProperties();
let props_ptr = self.this.as_ref().audioProperties();
let props_ref = unsafe {
// SAFETY:
// - This points to a C++ FFI type ensured to be aligned by cxx's codegen.

View file

@ -45,7 +45,7 @@ impl<'file_ref> PictureList<'file_ref> {
}
pub fn to_vec(&self) -> Vec<Picture<'file_ref>> {
let pictures = PictureList_to_vector(self.this.pin());
let pictures = PictureList_to_vector(self.this.as_ref());
let mut result = Vec::new();
for picture_ptr in pictures.iter() {
let picture_ptr = picture_ptr.get();
@ -71,7 +71,7 @@ impl<'file_ref> Picture<'file_ref> {
}
pub fn data(&self) -> Option<OwnedByteVector<'file_ref>> {
let data = Picture_data(self.this.pin());
let data = Picture_data(self.this.as_ref());
let this = unsafe { OwnedThis::new(data) };
this.map(|this| ByteVector::new(this))
}

View file

@ -12,38 +12,38 @@ impl<'file_ref> ID3v1Tag<'file_ref> {
}
pub fn title(&self) -> Option<OwnedString<'file_ref>> {
let title = bridge::ID3v1Tag_title(self.this.pin());
let title = bridge::ID3v1Tag_title(self.this.as_ref());
let string_this = unsafe { OwnedThis::new(title) };
string_this.map(|this| String::new(this))
}
pub fn artist(&self) -> Option<OwnedString<'file_ref>> {
let artist = bridge::ID3v1Tag_artist(self.this.pin());
let artist = bridge::ID3v1Tag_artist(self.this.as_ref());
let string_this = unsafe { OwnedThis::new(artist) };
string_this.map(|this| String::new(this))
}
pub fn album(&self) -> Option<OwnedString<'file_ref>> {
let album = bridge::ID3v1Tag_album(self.this.pin());
let album = bridge::ID3v1Tag_album(self.this.as_ref());
let string_this = unsafe { OwnedThis::new(album) };
string_this.map(|this| String::new(this))
}
pub fn comment(&self) -> Option<OwnedString<'file_ref>> {
let comment = bridge::ID3v1Tag_comment(self.this.pin());
let comment = bridge::ID3v1Tag_comment(self.this.as_ref());
let string_this = unsafe { OwnedThis::new(comment) };
string_this.map(|this| String::new(this))
}
pub fn genre_index(&self) -> u32 {
bridge::ID3v1Tag_genreIndex(self.this.pin())
bridge::ID3v1Tag_genreIndex(self.this.as_ref())
}
pub fn year(&self) -> u32 {
bridge::ID3v1Tag_year(self.this.pin())
bridge::ID3v1Tag_year(self.this.as_ref())
}
pub fn track(&self) -> u32 {
bridge::ID3v1Tag_track(self.this.pin())
bridge::ID3v1Tag_track(self.this.as_ref())
}
}

View file

@ -15,7 +15,7 @@ impl<'file_ref> ID3v2Tag<'file_ref> {
}
pub fn frames(&self) -> Option<FrameList<'file_ref>> {
let frames = bridge::Tag_frameList(self.this.pin());
let frames = bridge::Tag_frameList(self.this.as_ref());
let this = unsafe { OwnedThis::new(frames) };
this.map(|this| FrameList::new(this))
}
@ -31,7 +31,7 @@ impl<'file_ref> FrameList<'file_ref> {
}
pub fn to_vec(&self) -> Vec<Frame<'file_ref>> {
let frames = bridge::FrameList_to_vector(self.this.pin());
let frames = bridge::FrameList_to_vector(self.this.as_ref());
frames
.iter()
.map(|frame| {
@ -87,7 +87,7 @@ impl<'file_ref> TextIdentificationFrame<'file_ref> {
}
pub fn field_list(&self) -> Option<OwnedStringList<'file_ref>> {
let field_list = bridge::TextIdentificationFrame_fieldList(self.this.pin());
let field_list = bridge::TextIdentificationFrame_fieldList(self.this.as_ref());
let this = unsafe { OwnedThis::new(field_list) };
this.map(|this| StringList::new(this))
}
@ -103,7 +103,7 @@ impl<'file_ref> UserTextIdentificationFrame<'file_ref> {
}
pub fn values(&self) -> Option<OwnedStringList<'file_ref>> {
let values = bridge::UserTextIdentificationFrame_fieldList(self.this.pin());
let values = bridge::UserTextIdentificationFrame_fieldList(self.this.as_ref());
let this = unsafe { OwnedThis::new(values) };
this.map(|this| StringList::new(this))
}
@ -119,7 +119,7 @@ impl<'file_ref> AttachedPictureFrame<'file_ref> {
}
pub fn picture(&self) -> Option<OwnedByteVector<'file_ref>> {
let picture = bridge::AttachedPictureFrame_picture(self.this.pin());
let picture = bridge::AttachedPictureFrame_picture(self.this.as_ref());
let this = unsafe { OwnedThis::new(picture) };
this.map(|this| ByteVector::new(this))
}

View file

@ -15,7 +15,7 @@ impl<'file_ref> MP4Tag<'file_ref> {
}
pub fn item_map(&self) -> ItemMap<'file_ref> {
let map: &'file_ref CPPItemMap = self.this.pin().itemMap();
let map: &'file_ref CPPItemMap = self.this.as_ref().itemMap();
let map_this = unsafe { RefThis::new(map) };
ItemMap::new(map_this)
}
@ -31,7 +31,7 @@ impl<'file_ref> ItemMap<'file_ref> {
}
pub fn to_hashmap(&self) -> HashMap<String, MP4Item<'file_ref>> {
let cxx_vec = ItemMap_to_entries(self.this.pin());
let cxx_vec = ItemMap_to_entries(self.this.as_ref());
let vec: Vec<(String, MP4Item<'file_ref>)> =
cxx_vec.iter()
.map(|property| {
@ -41,12 +41,11 @@ impl<'file_ref> ItemMap<'file_ref> {
// not change address by C++ semantics.
// - The values returned are copied and thus not dependent on the address
// of self.
let property_pin = unsafe { Pin::new_unchecked(property) };
let key_ref = property_pin.key();
let key_ref = property.key();
let key_this = unsafe { RefThis::new(key_ref) };
let key = tk::String::new(key_this).to_string();
let value_ref = property_pin.value();
let value_ref = property.value();
let value_this = unsafe { RefThis::new(value_ref) };
let value = MP4Item::new(value_this);
@ -68,35 +67,35 @@ impl<'file_ref> MP4Item<'file_ref> {
}
pub fn data(&self) -> Option<MP4Data<'file_ref>> {
if !self.this.pin().isValid() {
if !self.this.as_ref().isValid() {
return None;
}
let item_type = MP4ItemType::from_u32(super::bridge::Item_type(self.this.pin()));
let item_type = MP4ItemType::from_u32(super::bridge::Item_type(self.this.as_ref()));
item_type.and_then(|item_type| match item_type {
MP4ItemType::Void => Some(MP4Data::Void),
MP4ItemType::Bool => Some(MP4Data::Bool(self.this.pin().toBool())),
MP4ItemType::Int => Some(MP4Data::Int(self.this.pin().toInt())),
MP4ItemType::Bool => Some(MP4Data::Bool(self.this.as_ref().toBool())),
MP4ItemType::Int => Some(MP4Data::Int(self.this.as_ref().toInt())),
MP4ItemType::IntPair => {
let pair = super::bridge::Item_toIntPair(self.this.pin());
let pair = super::bridge::Item_toIntPair(self.this.as_ref());
let pair_this = unsafe { OwnedThis::new(pair) };
pair_this.map(|this| MP4Data::IntPair(IntPair::new(this)))
},
MP4ItemType::Byte => Some(MP4Data::Byte(self.this.pin().toByte())),
MP4ItemType::UInt => Some(MP4Data::UInt(self.this.pin().toUInt())),
MP4ItemType::LongLong => Some(MP4Data::LongLong(super::bridge::Item_toLongLong(self.this.pin()))),
MP4ItemType::Byte => Some(MP4Data::Byte(self.this.as_ref().toByte())),
MP4ItemType::UInt => Some(MP4Data::UInt(self.this.as_ref().toUInt())),
MP4ItemType::LongLong => Some(MP4Data::LongLong(super::bridge::Item_toLongLong(self.this.as_ref()))),
MP4ItemType::StringList => {
let string_list = super::bridge::Item_toStringList(self.this.pin());
let string_list = super::bridge::Item_toStringList(self.this.as_ref());
let string_list_this = unsafe { OwnedThis::new(string_list) };
string_list_this.map(|this| MP4Data::StringList(tk::StringList::new(this)))
},
MP4ItemType::ByteVectorList => {
let byte_vector_list = super::bridge::Item_toByteVectorList(self.this.pin());
let byte_vector_list = super::bridge::Item_toByteVectorList(self.this.as_ref());
let byte_vector_list_this = unsafe { OwnedThis::new(byte_vector_list) };
byte_vector_list_this.map(|this| MP4Data::ByteVectorList(tk::ByteVectorList::new(this)))
},
MP4ItemType::CoverArtList => {
let cover_art_list = super::bridge::Item_toCoverArtList(self.this.pin());
let cover_art_list = super::bridge::Item_toCoverArtList(self.this.as_ref());
let cover_art_list_this = unsafe { OwnedThis::new(cover_art_list) };
cover_art_list_this.map(|this| MP4Data::CoverArtList(CoverArtList::new(this)))
}
@ -114,13 +113,12 @@ impl<'file_ref> CoverArtList<'file_ref> {
}
pub fn to_vec(&self) -> Vec<CoverArt> {
let cover_arts = self.this.pin().to_vector();
let cover_arts = self.this.as_ref().to_vector();
cover_arts
.iter()
.map(|ca| {
let ca_pin = unsafe { Pin::new_unchecked(ca) };
let format = CoverArtFormat::from_u32(ca_pin.format());
let data = ca_pin.data();
let format = CoverArtFormat::from_u32(ca.format());
let data = ca.data();
let data_this = unsafe { RefThis::new(&*data) };
let data = tk::ByteVector::new(data_this).to_vec();
CoverArt { format, data }
@ -139,7 +137,7 @@ impl<'file_ref> IntPair<'file_ref> {
}
pub fn to_tuple(&self) -> Option<(i32, i32)> {
let this = self.this.pin();
let this = self.this.as_ref();
let first = this.first();
let second = this.second();
Some((first, second))

View file

@ -13,7 +13,7 @@ impl<'file_ref> VorbisFile<'file_ref> {
}
pub fn xiph_comments(&self) -> Option<XiphComment<'file_ref>> {
let tag = self.this.pin().vorbisTag();
let tag = self.this.as_ref().vorbisTag();
let tag_ref = unsafe {
// SAFETY: This pointer is a valid type, and can only used and accessed
// via this function and thus cannot be mutated, satisfying the aliasing rules.
@ -34,7 +34,7 @@ impl<'file_ref> OpusFile<'file_ref> {
}
pub fn xiph_comments(&self) -> Option<XiphComment<'file_ref>> {
let tag = self.this.pin().opusTag();
let tag = self.this.as_ref().opusTag();
let tag_ref = unsafe {
// SAFETY: This pointer is a valid type, and can only used and accessed
// via this function and thus cannot be mutated, satisfying the aliasing rules.

View file

@ -4,24 +4,12 @@ use cxx::{UniquePtr, memory::UniquePtrTarget};
/// A taglib-FFI-specific trait representing a C++ object returned by the library.
///
/// This trait is used to provide a temporary pin of the object for use in C++
/// member function calls.
///
/// `This` instances must hold the following contract:
/// - This object will remain valid as long as TagLib's FileRef object is valid,
/// and will be dropped when the FileRef is dropped.
/// - This object will not move or be mutated over the FileRef's lifetime, this way
/// it can be temporarily pinned for use as a `this` pointer.
pub trait This<'file_ref, T> {
/// Temporarily pin the object for use in C++ member function calls.
///
/// When C++ member functions are called, the object is temporarily pinned
/// as a `this` pointer. In turn, cxx requires a `Pin<&T>` to be used in it's
/// member function bindings, hence this method.
///
/// This is safe to call assuming the contract of `This` is upheld.
fn pin(&self) -> Pin<&T>;
}
pub trait This<'file_ref, T> : AsRef<T> {}
/// A taglib-FFI-specific trait representing a C++ object returned by the library.
///
@ -66,12 +54,14 @@ impl<'file_ref, T> RefThis<'file_ref, T> {
}
}
impl<'file_ref, T> This<'file_ref, T> for RefThis<'file_ref, T> {
fn pin(&self) -> Pin<&T> {
unsafe { Pin::new_unchecked(self.this) }
impl<'file_ref, T> AsRef<T> for RefThis<'file_ref, T> {
fn as_ref(&self) -> &T {
self.this
}
}
impl<'file_ref, T> This<'file_ref, T> for RefThis<'file_ref, T> {}
/// A [ThisMut] instance that is a reference to a C++ object.
///
/// This is similar to [RefThis], but allows mutating the object.
@ -110,12 +100,14 @@ impl<'file_ref, T> RefThisMut<'file_ref, T> {
}
}
impl<'file_ref, T> This<'file_ref, T> for RefThisMut<'file_ref, T> {
fn pin(&self) -> Pin<&T> {
unsafe { Pin::new_unchecked(self.this) }
impl<'file_ref, T> AsRef<T> for RefThisMut<'file_ref, T> {
fn as_ref(&self) -> &T {
self.this
}
}
impl<'file_ref, T> This<'file_ref, T> for RefThisMut<'file_ref, T> {}
impl<'file_ref, T> ThisMut<'file_ref, T> for RefThisMut<'file_ref, T> {
fn pin_mut(&mut self) -> Pin<&mut T> {
unsafe { Pin::new_unchecked(self.this) }
@ -154,12 +146,14 @@ impl<'file_ref, T : UniquePtrTarget> OwnedThis<'file_ref, T> {
}
}
impl<'file_ref, T : UniquePtrTarget> This<'file_ref, T> for OwnedThis<'file_ref, T> {
fn pin(&self) -> Pin<&T> {
unsafe { Pin::new_unchecked(self.this.as_ref().unwrap()) }
impl<'file_ref, T : UniquePtrTarget> AsRef<T> for OwnedThis<'file_ref, T> {
fn as_ref(&self) -> &T {
self.this.as_ref().unwrap()
}
}
impl<'file_ref, T : UniquePtrTarget> This<'file_ref, T> for OwnedThis<'file_ref, T> {}
impl<'file_ref, T : UniquePtrTarget> ThisMut<'file_ref, T> for OwnedThis<'file_ref, T> {
fn pin_mut(&mut self) -> Pin<&mut T> {
self.this.as_mut().unwrap()

View file

@ -18,7 +18,7 @@ impl<'file_ref, T: This<'file_ref, CPPString>> String<'file_ref, T> {
impl<'file_ref, T: This<'file_ref, CPPString>> ToString for String<'file_ref, T> {
fn to_string(&self) -> std::string::String {
let c_str = self.this.pin().toCString(true);
let c_str = self.this.as_ref().toCString(true);
unsafe {
// SAFETY:
// - This is a C-string returned by a C++ method guaranteed to have
@ -54,7 +54,7 @@ impl<'file_ref, T: This<'file_ref, CPPStringList>> StringList<'file_ref, T> {
}
pub fn to_vec(&self) -> Vec<std::string::String> {
let cxx_values = bridge::StringList_to_vector(self.this.pin());
let cxx_values = bridge::StringList_to_vector(self.this.as_ref());
cxx_values
.iter()
.map(|value| {
@ -76,7 +76,7 @@ impl<'file_ref, T: This<'file_ref, CPPByteVector>> ByteVector<'file_ref, T> {
}
pub fn to_vec(&self) -> Vec<u8> {
let this = self.this.pin();
let this = self.this.as_ref();
let size = this.size().try_into().unwrap();
let data = this.data();
// Re-cast to u8
@ -106,7 +106,7 @@ impl<'file_ref, T: This<'file_ref, CPPByteVectorList>> ByteVectorList<'file_ref,
}
pub fn to_vec(&self) -> Vec<Vec<u8>> {
let cxx_values = bridge::ByteVectorList_to_vector(self.this.pin());
let cxx_values = bridge::ByteVectorList_to_vector(self.this.as_ref());
cxx_values
.iter()
.map(|value| ByteVector::new(unsafe { RefThis::new(value) }).to_vec())

View file

@ -16,7 +16,7 @@ impl<'file_ref> XiphComment<'file_ref> {
}
pub fn field_list_map(&self) -> FieldListMap<'file_ref> {
let map: &'file_ref CPPFieldListMap = self.this.pin().fieldListMap();
let map: &'file_ref CPPFieldListMap = self.this.as_ref().fieldListMap();
let map_this = unsafe { RefThis::new(map) };
FieldListMap::new(map_this)
}
@ -40,7 +40,7 @@ impl<'file_ref> FieldListMap<'file_ref> {
impl<'file_ref> FieldListMap<'file_ref> {
pub fn to_hashmap(&self) -> HashMap<String, tk::RefStringList<'file_ref>> {
let cxx_vec = FieldListMap_to_entries(self.this.pin());
let cxx_vec = FieldListMap_to_entries(self.this.as_ref());
cxx_vec
.iter()
.map(|property| {
@ -50,11 +50,10 @@ impl<'file_ref> FieldListMap<'file_ref> {
// not change address by C++ semantics.
// - The values returned are copied and thus not dependent on the address
// of self.
let property_pin = unsafe { Pin::new_unchecked(property) };
let key_ref = property_pin.key();
let key_ref = property.key();
let key_this = unsafe { RefThis::new(key_ref) };
let key = tk::String::new(key_this).to_string();
let value_ref = property_pin.value();
let value_ref = property.value();
let value_this = unsafe { RefThis::new(value_ref) };
let value = tk::StringList::new(value_this);
(key, value)