pub struct Device { /* private fields */ }Expand description
An opened device connected via USB.
Implementations§
Source§impl Device
impl Device
Sourcepub fn from_serial(serial: &str) -> Result<Option<Self>>
pub fn from_serial(serial: &str) -> Result<Option<Self>>
Searches the device by the serial number.
§Errors
Returns an error if the operation has failed.
§Examples
use libmtp::Device;
if let Some(device) = Device::from_serial("GVEV4I3E0WU1")? {
println!("{device:?}");
} else {
println!("Device not found");
}Examples found in repository?
More examples
4fn main() -> libmtp::Result<()> {
5 let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
6 let storage = device.find_storage(65537).expect("Storage should exist");
7 for object in storage.iter_recursive() {
8 if let Object::File(file) = object {
9 let path = format!("/tmp/libmtp-{}", file.name());
10 file.copy_to_host(path)?;
11 break;
12 }
13 }
14 Ok(())
15}Sourcepub fn serial(&self) -> Result<String>
pub fn serial(&self) -> Result<String>
Retrieves the serial number of the device.
§Panics
Panics if the serial number of the device is not a valid UTF-8.
§Errors
Returns an error if the operation has failed.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
assert_eq!(device.serial()?, "GVEV4I3E0WU1");Sourcepub fn name(&self) -> Result<String>
pub fn name(&self) -> Result<String>
Retieves the friendly name of the device.
§Errors
Returns an error if the device doesn’t have a support for friendly names, if the friendly name was not found or if the operation has failed.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
println!("{}", device.name()?);Sourcepub fn music_folder_id(&self) -> Option<u32>
pub fn music_folder_id(&self) -> Option<u32>
Retrieves the ID of the default music folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.music_folder_id() {
println!("Music folder ID: {id}");
} else {
println!("Music folder not found");
}Sourcepub fn playlist_folder_id(&self) -> Option<u32>
pub fn playlist_folder_id(&self) -> Option<u32>
Retrieves the ID of the default playlists folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.playlist_folder_id() {
println!("Playlists folder ID: {id}");
} else {
println!("Playlists folder not found");
}Sourcepub fn picture_folder_id(&self) -> Option<u32>
pub fn picture_folder_id(&self) -> Option<u32>
Retrieves the ID of the default pictures folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.picture_folder_id() {
println!("Pictures folder ID: {id}");
} else {
println!("Pictures folder not found");
}Sourcepub fn video_folder_id(&self) -> Option<u32>
pub fn video_folder_id(&self) -> Option<u32>
Retrieves the ID of the default videos folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.video_folder_id() {
println!("Videos folder ID: {id}");
} else {
println!("Videos folder not found");
}Sourcepub fn organizer_folder_id(&self) -> Option<u32>
pub fn organizer_folder_id(&self) -> Option<u32>
Retrieves the ID of the default organizers folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.organizer_folder_id() {
println!("Organizers folder ID: {id}");
} else {
println!("Organizers folder not found");
}Sourcepub fn zencast_folder_id(&self) -> Option<u32>
pub fn zencast_folder_id(&self) -> Option<u32>
Retrieves the ID of the default ZENcast folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.zencast_folder_id() {
println!("ZENcast folder ID: {id}");
} else {
println!("ZENcast folder not found");
}Sourcepub fn album_folder_id(&self) -> Option<u32>
pub fn album_folder_id(&self) -> Option<u32>
Retrieves the ID of the default albums folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.album_folder_id() {
println!("Albums folder ID: {id}");
} else {
println!("Albums folder not found");
}Sourcepub fn text_folder_id(&self) -> Option<u32>
pub fn text_folder_id(&self) -> Option<u32>
Retrieves the ID of the default texts folder of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(id) = device.text_folder_id() {
println!("Texts folder ID: {id}");
} else {
println!("Texts folder not found");
}Sourcepub fn refresh(&self) -> Result<()>
pub fn refresh(&self) -> Result<()>
Refreshes storages information for the device.
§Errors
Returns an error if the operation has failed.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
let storage = device.find_storage(65537).expect("Storage should exist");
println!("Before: {}", storage.free_space());
device.refresh()?;
println!("After: {}", storage.free_space());Sourcepub fn rename(&self, name: &str) -> Result<()>
pub fn rename(&self, name: &str) -> Result<()>
Changes the friendly name of the device.
§Errors
Returns an error if the device doesn’t have a support for friendly names or if the operation has failed.
§Panics
Panics if the friendly name of the storage contains a nul byte.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
device.rename("Bob's Phone")?;Sourcepub fn iter(&self) -> StorageIter<'_> ⓘ
pub fn iter(&self) -> StorageIter<'_> ⓘ
Retrieves an iterator over the storages of the device.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
for storage in &device {
println!("{storage:?}");
}Sourcepub fn find_storage(&self, id: u32) -> Option<Storage<'_>>
pub fn find_storage(&self, id: u32) -> Option<Storage<'_>>
Searches the storage of the device by the ID.
§Examples
use libmtp::Device;
let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
if let Some(storage) = device.find_storage(65537) {
println!("{storage:?}");
} else {
println!("Storage not found");
}Examples found in repository?
More examples
4fn main() -> libmtp::Result<()> {
5 let device = Device::from_serial("GVEV4I3E0WU1")?.expect("Device should exist");
6 let storage = device.find_storage(65537).expect("Storage should exist");
7 for object in storage.iter_recursive() {
8 if let Object::File(file) = object {
9 let path = format!("/tmp/libmtp-{}", file.name());
10 file.copy_to_host(path)?;
11 break;
12 }
13 }
14 Ok(())
15}